Passing special chars from java to jsp in XML format
I am fetching data from DB which contains "&". I am writing it into stringbuffer and passing it in xml format to jsp. In jsp when i get the doc.getElementByTagN开发者_JAVA百科ame("tagname") and fetchin the childnode length i am getting null.
XML data contains "&". The data i am fetching is huge. So what could be the most effective way to replace "&" with & in java
<parentnode>
<childnode>
<Data1>A & B</Data1>C & D<Data2></Data2>
<Data1>E & F</Data1><Data2></Data2>
<Data1></Data1><Data2></Data2>
..
..
</childnode>
</parentnode>
You need to escape it. Since you seem to print XML plain in JSP (which is a poor approach, but that aside), you can use the JSTL <c:out>
for this.
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${bean.someXmlString}" />
Alternatively, you can also escape it in the Java side using Apache Commons Lang StringEscapeUtils#escapeXml()
.
stringBuilder.append(StringEscapeUtils.escapeXml(someXmlString));
// ...
精彩评论