Mike
Mike

Reputation: 1899

How to write special character ( &, <,> etc ) in xml using dom parser

I want to create an xml file which have '&' in one of its text node. like

<url>www.sample.com/where/values?firstvalue=1&secondvalue=2</url>

Now what I am getting is

<url>www.sample.com/where/values?firstvalue=1&amp;secondvalue=2</url>

Could you please let me know how can I achieve that. I am attaching the url value to dom using contenturl.appendChild(doc.createTextNode(model.getDocURL()));

and I am parsing using following method

        DOMSource source = new DOMSource(doc);
        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");    
        transformer.transform(source, result);

Upvotes: 0

Views: 4050

Answers (2)

bobwilmes
bobwilmes

Reputation: 64

Have you tried escaping the ampersand character as "...firstvalue=1\&#x0Dsecondvalue=2..." ?

Upvotes: -1

thagorn
thagorn

Reputation: 747

I agree with Rubens Farias that you probably want to leave it as &amp; which is a correctly escaped ampersand. However this question deals with unescaping html characters:

Convert HTML Character Back to Text Using Java Standard Library

Upvotes: 2

Related Questions