Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML parsing issue with '&' in element text

Tags:

java

parsing

xml

I have the following code:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(inputXml)));

And the parse step is throwning:

SAXParseException: The entity name must immediately follow 
                   the '&' in the entity reference

due to the following '&' in my inputXml:

<Line1>Day & Night</Line1>

I'm not in control of in the inbound XML. How can I safely/correctly parse this?

like image 993
Chris Knight Avatar asked Oct 01 '10 10:10

Chris Knight


1 Answers

Quite simply, the input "XML" is not valid XML. The entity should be encoded, i.e.:

<Line1>Day &amp; Night</Line1>

Basically, there's no "proper" way to fix this other than telling the XML supplier that they're giving you garbage and getting them to fix it. If you're in some horrible situation where you've just got to deal with it, then the approach you take will likely depend on what range of values you're expected to receive.

If there's no entities in the document at all, a regex replace of & with &amp; before processing would do the trick. But if they're sending some entities correctly, you'd need to exclude these from the matching. And on the rare chance that they actually wanted to send the entity code (i.e. sent &amp; but meant &amp;amp;) you're going to be completely out of luck.

But hey - it's the supplier's fault anyway, and if your attempt to fix up invalid input isn't exactly what they wanted, there's a simple thing they can do to address that. :-)

like image 57
Andrzej Doyle Avatar answered Oct 02 '22 14:10

Andrzej Doyle