woezelmann woezelmann - 27 days ago 15
Java Question

SAXReader not re-ecape characters

I'm reading a XML file with dom4j. The file looks like this:

...
<Field>&#13;&#10; hello, world...</Field>
...


I read the file with
SAXReader
into a
Document
. When I use
getText()
on a the node I obtain the followin String:

\r\n hello, world...


I do some processing and then write another file using
asXml()
. But the characters are not escaped as in the original file which results in error in the external system which uses the file.

How can I escape the special character and have
&#13;&#10;
when writing the file?

Answer

You cannot easily. Those aren't 'escapes', they are 'character entities'. They are a fundamental part of XML. Xerces has some very complex support for 'unparsed entities', but I doubt that it applies to these, as opposed to the species that are defined in a DTD.

Comments