Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java XML processing entity problem?

I get the following error when I try to run my java program(it's supposed to read an xml file and print out some of the content).

From what I understand there is an unreferenced entity which is not part of the xml standard so my question is; how can I fix this problem?

Thanks,

[Fatal Error] subject.xml:4:233: The entity "rsquo" was referenced, but not declared.
org.xml.sax.SAXParseException: The entity "rsquo" was referenced, but not declared.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at DomParserExample2.parseXmlFile(DomParserExample2.java:42)
at DomParserExample2.runExample(DomParserExample2.java:24)
at DomParserExample2.main(DomParserExample2.java:115)
Exception in thread "main" java.lang.NullPointerException
at DomParserExample2.parseDocument(DomParserExample2.java:54)
at DomParserExample2.runExample(DomParserExample2.java:27)
at DomParserExample2.main(DomParserExample2.java:115)
like image 385
anonymous Avatar asked Nov 04 '10 09:11

anonymous


2 Answers

The entity ’ is not an XML-Entity. Its defined in HTML: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

If you created the XML you can add entitiess to your DTD. To fix the issue, add a DTD to the XML file (if not already defined).

XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE demo SYSTEM "./demo.dtd">
<demo>
    &rsquo;
</demo>

DTD:

<!ELEMENT demo (#PCDATA)>
<!ENTITY rsquo   "&#8217;">

Provide the DTD to the application and the error goes away. I wouldn't write all entities myself, I would use one from W3C, such as: http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent

How to include the DTD for your XML is another Question. As far as I remember you can set the path to the DTD, or an Catalog-File.

edit 2: Take a look at the EntityResolver: http://download.oracle.com/javase/1.4.2/docs/api/org/xml/sax/EntityResolver.html

like image 98
Christian Kuetbach Avatar answered Oct 23 '22 14:10

Christian Kuetbach


Following the answer of Christian, you also have the possibility to declare your entities right into the XML

<!DOCTYPE your_type [
   <!ENTITY rsquo "&#8217;">
   <!ENTITY lsquo "&#8216;">
]>
like image 31
Marc Avatar answered Oct 23 '22 14:10

Marc