We help IT Professionals succeed at work.

How to turn those &lt; to <  and &gt; to > from StringBuffer.toString, struts2 and sarissa

JianJunShen
JianJunShen asked
on
515 Views
Last Modified: 2013-11-24
Hi; I have a search string which is generated by action and put in request scope. Then in the result page, I put this string in jsp. So that I could use Sarissa to parse it as XML. The problem, when search string is returned, it is not <items><item><name>somename</name></item></items>, it is something like &lt;items&gt;&lt;item&gt;&lt;name&gt; ...

In Sarissa, when I read xtring by document.getElementById("xmlDocument") and then I could not parse it. It is not a valid XML. Does someone know how to get string with '<','>', not '&lt;' and '&gt;'.

My action codes

public String execute(){
            List<Item> items = registrationService.searchItem(getSearchQuery());
            String xmlString = generateXML(items);
            request.put("xmlString", xmlString);
            return "searchResult";
      }

      private String generateXML(List<Item> items) {
            int size = items.size();
            StringBuffer xmlString = new StringBuffer("<items>");
            for(int i=0;i<size;i++){
               xmlString.append("<item>");       
               Item item = (Item)items.get(i);      
               xmlString.append("<name>"+item.getName()+"</name>");
               xmlString.append("<price>"+item.getInitprice()+"</price>");
               xmlString.append("</item>");
            }
            xmlString.append("</items>");
            return xmlString.toString();
      }

And my javascript is as follows:
                     function init()
         {
          // create the xml document object
             oXmlDoc = Sarissa.getDomDocument();
             oXmlDoc.async = false;
             var xmlString = document.getElementById("xmlDocument").innerHTML;
             alert(xmlString);
             oXmlDoc = (new DOMParser()).parseFromString(xmlString, "text/xml");

             // create the xml document object
             oXslDoc = Sarissa.getDomDocument();
             oXslDoc.async = false;
             oXslDoc.load("item.xsl");                //
             showTable('NAME', '1', showCounter,'ascending'); //
        }

Comment
Watch Question

Have you tried using any escape sequence ?
For example use
xmlString.append("\<item\>");      
instead of
xmlString.append("<item>");      

Hope this helps

Author

Commented:
It does not even go through Elicpse compilation. It says \< is not an escape sequence.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Do you know how to turn parser generated XML into String which keeps original symbol '<' or '>'? The example explains how to generated into a file. Am I right?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
May I use also use stringWriter instead of fileoutputstream in 2nd example in the link. Since it is DOM model, more easy to understand. Anyway, I will try tomorrow.

Author

Commented:
I finally find the reason. When user request.put("xmlString",someXmlString), in JSP, it will automatically turn to &lt; or &gt; symbol. The way I conquer this method is ajax. Dojo RPC. It needs JSON plug in. At that time, when using brunoguimaraes's link. It works. Thanks.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.