简体   繁体   English

使用Java生成带有“&”的xml文件

[英]Generating xml file with “&” using java

I need to write <name> Dolce & Gabanna </name> in an xml file using java. 我需要使用Java在xml文件中编写<name> Dolce & Gabanna </name> However, what gets generated is <name> Dolce &amp; Gabanna </name> 但是,生成的是<name> Dolce &amp; Gabanna </name> <name> Dolce &amp; Gabanna </name> . <name> Dolce &amp; Gabanna </name>

Is there any method I can use in java so "&" will be displayed in xml, instead of "&amp;" 我有什么方法可以在Java中使用,因此"&"将显示在xml中,而不是"&amp;" ?

Thanks. 谢谢。

I need to write <name> Dolce & Gabanna </name> in an xml file using java. 我需要使用Java在xml文件中编写<name> Dolce & Gabanna </name>

No, you don't. 不,你没有。 Not unless you're trying to create an invalid file. 除非您试图创建一个无效的文件,否则不会。 Whatever code you're using is doing exactly the right thing - any XML parser reading the file will then unescape the &amp; 无论您使用什么代码,都可以做正确的事- 读取该文件的任何XML解析器都会取消&amp; to & where appropriate. &在适当的地方。

From the XML 1.0 specification, section 2.4 : 从XML 1.0规范的2.4节开始

The ampersand character ( & ) and the left angle bracket ( < ) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. 除用于标记分隔符时,或在注释,处理指令或CDATA节中,“&”字符( & )和左尖括号( < )均不得以其文字形式出现。 If they are needed elsewhere, they must be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively. 如果在其他地方需要它们,则必须使用数字字符引用或分别使用字符串“ &amp; ”和“ &lt; ”对其进行转义。

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

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