Problem processing Java XML data?

I get the following error when trying to run my java program (it should read the xml file and print some of the content).

From what I understand, there is an unreferenced object that 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) 
+7
java xml entity entityresolver
source share
3 answers

Object ’ not an XML Entity. It is defined in HTML: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

If you created XML, you can add Entitys to the DTD.

Something like this might help: http://gv.ca/dtd/character-entities.dtd

edit: To fix this problem, you can add a DTD to your XML file (if it is not already defined).

Your XML:

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

Your DTD:

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

If you provide a DTD for your application, the error will disappear. I would not write all the Entites myself, I would use one of the W3C http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent

How to enable DTD for your XML is another question. As far as I remember, you can set the path to the DTD or the directory file.

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

+6
source share

Following the Christian answer, you also have the opportunity to declare your entities directly in XML

 <!DOCTYPE your_type [ <!ENTITY rsquo "&#8217;"> <!ENTITY lsquo "&#8216;"> ]> 
+3
source share
 /** * This Inner class is written to solve the XML parsing DTD validation * checking from online because if Internet is not connected, then it * throws Exception. * * @author Ravi Thapa */ public class CustomEntityResolver implements EntityResolver { public InputSource resolveEntity(String publicId, String systemId) { InputSource source = null; Pattern pattern1 = Pattern.compile("^-//(.*)//DTD(.*)$", Pattern.CASE_INSENSITIVE); Matcher match1 = pattern1.matcher(publicId.trim()); Pattern pattern2 = Pattern.compile("^http://(.*).dtd$", Pattern.CASE_INSENSITIVE); Matcher match2 = pattern2.matcher(systemId.trim()); if (match1.find() || match2.find()) { source = new InputSource(new ByteArrayInputStream("".getBytes())); } // return null to signal default behavior return source; } } 
0
source share

All Articles