Java XML processing entity problem?

牧云@^-^@ 提交于 2020-01-11 04:49:26

问题


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)

回答1:


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 Entitys to you DTD.

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

edit: To fix this issue you can add an DTD to your XML File (if 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 the DTD to your Application, the error goes away. I wouldn't write alle Entites myself, I would use one from W3C 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




回答2:


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;">
]>



回答3:


/**
         * 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;
        }
    }


来源:https://stackoverflow.com/questions/4095451/java-xml-processing-entity-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!