Ignoring DTD when parsing XML

后端 未结 2 1443
Happy的楠姐
Happy的楠姐 2021-01-17 17:17

How can I ignore the DTD declaration when parsing file with XOM xml library. My file has the following line :




        
2条回答
  •  灰色年华
    2021-01-17 17:46

    The preferred solution would be to implement an EntityResolver that intercepts requests for the DTD and redirects these to an embedded copy. If you

    1. don't have access to the DTD and
    2. are absolutely sure you won't need it (apart from validation it might also declare character entities that are used in the document) and
    3. you are using the Xerces XML Parser implementation

    you can disable fetching of DTD by setting the corresponding SAX feature. In XOM this should be possible by passing an XMLReader to the Builder constructor like this:

    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLReaderFactory;
    
    ...
    
    XMLReader xmlreader = XMLReaderFactory.createXMLReader();
    xmlreader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    Builder builder = new Builder(xmlreader);
    

提交回复
热议问题