JianJunShen
asked on
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>somenam e</name></ item></ite ms>, it is something like <items><item>& lt;name> ; ...
In Sarissa, when I read xtring by document.getElementById("x mlDocument ") and then I could not parse it. It is not a valid XML. Does someone know how to get string with '<','>', not '<' and '>'.
My action codes
public String execute(){
List<Item> items = registrationService.search Item(getSe archQuery( ));
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.getNa me()+"</na me>");
xmlString.append("<price>" +item.getI nitprice() +"</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("x mlDocument ").innerHT ML;
alert(xmlString);
oXmlDoc = (new DOMParser()).parseFromStri ng(xmlStri ng, "text/xml");
// create the xml document object
oXslDoc = Sarissa.getDomDocument();
oXslDoc.async = false;
oXslDoc.load("item.xsl"); //
showTable('NAME', '1', showCounter,'ascending'); //
}
In Sarissa, when I read xtring by document.getElementById("x
My action codes
public String execute(){
List<Item> items = registrationService.search
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>"+
xmlString.append("<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("x
alert(xmlString);
oXmlDoc = (new DOMParser()).parseFromStri
// create the xml document object
oXslDoc = Sarissa.getDomDocument();
oXslDoc.async = false;
oXslDoc.load("item.xsl"); //
showTable('NAME', '1', showCounter,'ascending'); //
}
ASKER
It does not even go through Elicpse compilation. It says \< is not an escape sequence.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
I finally find the reason. When user request.put("xmlString",so meXmlStrin g), in JSP, it will automatically turn to < or > 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.
For example use
xmlString.append("\<item\>
instead of
xmlString.append("<item>")
Hope this helps