Skip to content

Java XML Document converting " to “(literal quote) upon parsing/converting to Document

I have this problem where I need to send to soap webservice that requires the root tag to have an xml data, this the xml that I’m trying to send:

<root>&lt;test key=&quot;Applicants&quot;&gt;this is a data&lt;/test&gt;</root>

I need to append this to the SoapBody object as a document with this code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
factory.setNamespaceAware(true); 
factory.setExpandEntityReferences(false); 

DocumentBuilder builder = factory.newDocumentBuilder(); 
Document result = builder.parse(new ByteArrayInputStream(request.getRequest().getBytes()));

Then adding it to the SoapBody to be sent to the webservice.

However, upon sending this request and tracing the logs, it’s actually reverting the &quot; character to literal quotes (“)

This is the xml being sent:

<root>&lt;test key="Applicants"&gt;this is a data&lt;/test&gt;</root> 

As you can see, the ” is being transformed to literal quotes, how can I keep the original data within root tag (which has the &quot;)? It seems to be transforming it when I’m converting it to a Document object.

Would appreciate any help. Thanks.

Edit:

The webservice actually requires this format (from their documentation and sample xml requests), if this isn’t possible, is it a limitation? Should I user another framework?

Answer

The " and &quot; are completely equivalent in this context. You haven’t actually said whether this is causing a problem: if it is, then it’s because some recipient of the XML isn’t processing it correctly. Incidentally, it would also be legitimate to convert the &gt; to >.

When you parse XML and re-serialise it, irrelevant details like redundant whitespace get lost – just as if you copy this text into your text editor, the line-wrapping and font size gets lost.