[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

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

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'); //
        }

0
JianJunShen
Asked:
JianJunShen
  • 4
  • 2
2 Solutions
 
m_tawfickCommented:
Have you tried using any escape sequence ?
For example use
xmlString.append("\<item\>");      
instead of
xmlString.append("<item>");      

Hope this helps
0
 
JianJunShenAuthor Commented:
It does not even go through Elicpse compilation. It says \< is not an escape sequence.
0
 
brunoguimaraesCommented:
It doesn't work. You have to use a XML library like Xerces to generate the XML.

This link describes your problem and has some examples with different libraries: http://www.javazoom.net/services/newsletter/xmlgeneration.html
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
JianJunShenAuthor 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?
0
 
brunoguimaraesCommented:
In the third example, you can use another constructor, passing a StringWriter instead of a FileOutputStream.

StringWriter stringWriter;
XMLSerializer serializer = new XMLSerializer(stringWriter,of);

Then you call stringWriter.getBuffer().toString() to obtain the resulting String.
0
 
JianJunShenAuthor 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.
0
 
JianJunShenAuthor 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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now