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

Posted on 2007-10-04
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++){
               Item item = (Item)items.get(i);      
            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;
             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'); //

Question by:JianJunShen
    LVL 2

    Expert Comment

    Have you tried using any escape sequence ?
    For example use
    instead of

    Hope this helps

    Author Comment

    It does not even go through Elicpse compilation. It says \< is not an escape sequence.
    LVL 9

    Accepted Solution

    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:

    Author Comment

    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?
    LVL 9

    Assisted Solution

    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.

    Author Comment

    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 Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Problem to setup 18 65
    Logs to delete in Windows VCenter server ? 11 87
    triangle challenge 4 34
    countX 22 48
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now