Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML file with ä

Tags:

java

xml

I have a XML file that roughly looks like this:

<customer>
    <name>M&uuml;ller</name>
</customer>

I parse the file using following code:

File xmlFile = new File("file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile)

And get the error that the entity &uuml; is referenced but not declared. What I want is that the entry is being read but not parsed in any way, I want to get the value as it is written in the file.

How do I do that?

like image 229
user1406177 Avatar asked Jun 09 '15 12:06

user1406177


1 Answers

I tried setting:

dbFactory.setExpandEntityReferences(false);

but this doesn't work.

If you can't modify your xml content (using UTF-8 the xml can contain u umlaut), you might be able to add a DTD:

<!DOCTYPE definition [
<!ENTITY uuml "&#xfc;">
]>

If you can't modify your xml file, load the xml contents and prepend the DTD:

String dtd = "<!DOCTYPE definition [\n<!ENTITY uuml '&#xfc;'>\n]>\n",
            contents = <load xmlFile>;
Reader reader = new StringReader(dtd + contents);
InputSource src = new InputSource(reader);
Document doc = dBuilder.parse(src);
like image 190
Hummeling Engineering BV Avatar answered Nov 14 '22 22:11

Hummeling Engineering BV