简体   繁体   English

使用转义的'\\ r \\ n \\'将dom4j文档写入xml文件

[英]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: 编写一个dom4j文档,其中包含如下所示的标签:

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

to a xml file, but the \\r\\n should be escaped to &#13;&#10; 到xml文件,但是\\r\\n应该转义为&#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; 假设您的意思是文本节点中的CRLF序列(而不仅仅是文字反斜杠-r-反斜杠-n),那么您将无法说服XML序列化器将其写为&#13;&#10; , because XML says you don't have to. ,因为XML表示您不必这样做。 The document is absolutely equivalent in XML terms, whether you escape it or not. 无论是否转义,该文档在XML方面都是绝对等效的。 The only place you need to escape the CRLF sequence as &#13;&#10; 您唯一需要对CRLF序列进行转义的位置是&#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. 如果确实必须产生此输出,则必须编写自己的XML序列化器,该序列化器遵循特殊的规则来转义控制代码。 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. 但是,如果您这样做是因为外部工具无法读取带有CRLF序列的XML元素,则应集中精力修复该工具,因为如果它不能处理文本内容中的换行符,则该工具将损坏,并且不是正确的XML解析器。 。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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