0

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);
Mike
  • 1,889
  • 5
  • 26
  • 32
  • 2
    If you *serialise* the DOM in the first form, you will have a non well-formed XML document, and XML processors will be unable to read it. – Alohci Jan 19 '12 at 00:24
  • 2
    There is nothing wrong with `&` on your node; `&` is a reserved character and needs to be encoded on your XML output. If you read that string as XML (i.e., through a XML parser), everything will be fine. – Rubens Farias Jan 19 '12 at 00:31
  • Agreed. I don't know why you would want to create poorly formed xml. – Rob Marrowstone Jan 19 '12 at 00:59
  • Hi Guys, I am totaly agree with you that it would be a poorely formed XML but this is a management request and I can not do anything ... they want in that format so I am requesting that with you guys. – Mike Jan 19 '12 at 04:02

2 Answers2

2

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

Community
  • 1
  • 1
thagorn
  • 727
  • 7
  • 14
-1

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

bobwilmes
  • 64
  • 6