I am creating an XML file using Java and am then reading the data from it too. The data I am adding as text node contains <p> at several places, but as soon I try to read it, the string terminates on encountering <. What am I doing wrong? Would using escape sequence be helpful.?
-
1Show some code, and we can perhaps tell you what's wrong with it.Don Roby– Don Roby2011-04-16 10:40:00 +00:00Commented Apr 16, 2011 at 10:40
Add a comment
|
2 Answers
You need to use entities for < and > as they are reserved characters. Use < and > to replace the angle brackets.
XML has other reserved characters like & also.
4 Comments
Logan
You mean that while storing the data instead of entering <p> I should insert <p> ? Cause even when I did it, it didn't work. While reading it still terminates.
Tomas McGuinness
Post a snippet of XML that is causing the problem.
Logan
Manish Jain Rocks < p > Always. This is the data in the xml. When I read it, it terminated by giving output as: Manish Jain Rocks
Roland Illig
@Logan: You should put this additional information into the question, not into comments. Show us the code that reads and writes the XML, and some typical input data.
You could use StringEscapeUtils.escapeXml() from Apache Commons Lang to do escaping. However you shouldn't need to deal with escaping if you're using any library to read and write your XML. If you're constructing the XML entirely with your own strings then you should reconsider that approach.
1 Comment
StaxMan
Agreed. The best way to generate XML is to use XML tools; Stax XMLStreamWriter (from javax.xml.stream) is pretty good for this, simple, efficient.