tags:

views:

287

answers:

2

Sorry for asking about quite the same issue, but now i would like to:

write a dom4j Document which contains tags looking like this:

<Field>\r\n some text</Field>

to a xml file, but the \r\n should be escaped to &#13;&#10;

org.dom4j.Document.asXml()

does not work.

A: 

Walk the tree, applying String.replace to the Text nodes.

Emyr
woezelmann
Does it not let you insert real CRLFs?
Emyr
+1  A: 

Assuming you mean that's a CRLF sequence in the text node (and not merely a literal backslash-r-backslash-n), you won't be able to persuade an XML serialiser to write them as &#13;&#10;, because XML says you don't have to. The document is absolutely equivalent in XML terms, whether you escape it or not. The only place you need to escape the CRLF sequence as &#13;&#10; is in an attribute value.

If you really must produce this output, you would have to write your own XML serialiser that followed special rules for escaping control codes. But if you are doing this because an external tool can't read the XML element with CRLF sequences in, you should concentrate on fixing that tool, because if it can't deal with newlines in text content it's broken and not a proper XML parser.

bobince
this is not about escaping but about text lenght. As String the text is 2048 long, but after writing it to xml it's suddenly 2182. I just thought it has something to do with escaping
woezelmann