Ignoring DTD References in DocumentBuilder.parse()
When parsing XML files containing references to external DTDs, you may encounter errors if the DTD is unavailable. To avoid these errors and focus solely on parsing the XML content, consider the following solution:
<code class="java">DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // Disable validation and related features dbf.setValidating(false); dbf.setNamespaceAware(true); dbf.setFeature("http://xml.org/sax/features/namespaces", false); dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(f);</code>
By disabling these features, the parser will ignore DTD references and proceed with parsing the XML content.
Note that specific options may vary depending on the XML parser implementation. Refer to the documentation for your specific parser for more information on disabling DTD-related features.
The above is the detailed content of How to Ignore DTD References in DocumentBuilder.parse()?. For more information, please follow other related articles on the PHP Chinese website!