Link to home
Start Free TrialLog in
Avatar of JianJunShen
JianJunShen

asked on

How to turn those < to < and > 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'); //
        }

Avatar of m_tawfick
m_tawfick

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

Hope this helps
Avatar of JianJunShen

ASKER

It does not even go through Elicpse compilation. It says \< is not an escape sequence.
ASKER CERTIFIED SOLUTION
Avatar of brunoguimaraes
brunoguimaraes
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.