Java – XML file with & auml;

I have an XML file roughly as follows:

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

I use the following code to parse the file:

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

And get the error of the entity uuml; Referenced but undeclared What I want is to read the entry but not parse it in any way. I want to get the value written in the file

What do I do?

Solution

I tried setting:

dbFactory.setExpandEntityReferences(false);

But it doesn't work

If you can't modify your XML content (using UTF-8, XML can contain your umlaut), you can add a DTD:

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

If you cannot modify the XML file, load the XML content and add 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>