简体   繁体   中英

Write dom4j Document to xml-file with escaped '\r\n\'

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.

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.

遍历树,将String.replace应用于Text节点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM