0

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.?

1
  • 1
    Show some code, and we can perhaps tell you what's wrong with it. Commented Apr 16, 2011 at 10:40

2 Answers 2

1

You need to use entities for < and > as they are reserved characters. Use &lt; and &gt; to replace the angle brackets.

XML has other reserved characters like & also.

Sign up to request clarification or add additional context in comments.

4 Comments

You mean that while storing the data instead of entering <p> I should insert &lt;p&gt; ? Cause even when I did it, it didn't work. While reading it still terminates.
Post a snippet of XML that is causing the problem.
Manish Jain Rocks &lt; p &gt; Always. This is the data in the xml. When I read it, it terminated by giving output as: Manish Jain Rocks
@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.
0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.