Generating xml file with "&" using java

I need to write <name> Dolce & Gabanna </name> in an xml file using java. However, what gets generated is <name> Dolce &amp; Gabanna </name>.

Is there any method I can use in java so "&" will be displayed in xml, instead of "&amp;"?

Thanks.

Jon Skeet
people
quotationmark

I need to write <name> Dolce & Gabanna </name> in an xml file using java.

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; to & where appropriate.

From the XML 1.0 specification, section 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. If they are needed elsewhere, they must be escaped using either numeric character references or the strings " &amp; " and " &lt; " respectively.

people

See more on this question at Stackoverflow