How to use dom4j SAXReader offline?

I would like to work with SAXReader offline , the problem is that SAXReader checks for xml DTD compliance. I do not want to change DTD or anything else in XML. From a search on this website and other sources, I found 2 answers that did not help me :

  • use EntityResolver to bypass network call
  • use setIncludeExternalDTDDeclarations (false)

An example of what I was trying to do:

protected Document getPlistDocument() throws MalformedURLException, DocumentException { SAXReader saxReader = new SAXReader(); saxReader.setIgnoreComments(false); saxReader.setIncludeExternalDTDDeclarations(false); saxReader.setIncludeInternalDTDDeclarations(true); saxReader.setEntityResolver(new MyResolver()); Document plistDocument = saxReader.read(getDestinationFile().toURI().toURL()); return plistDocument; } public class MyResolver implements EntityResolver { public InputSource resolveEntity (String publicId, String systemId) { if (systemId.equals("http://www.myhost.com/today")) { // if we want a custom implementation, return a special input source return null; } else { // use the default behaviour return null; } } } 

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> 

I still can’t work offline, I advise ... thanks

Stacktrace:

 14:20:44,358 ERROR [ApplicationBuilder] iphone build failed: Resource Manager - Problem handle Root.plist: www.apple.com Nested exception: www.apple.com com.something.builder.sourcemanager.exception.SourceHandlingException: Resource Manager - Problem handle Root.plist: www.apple.com Nested exception: www.apple.com **** **** Caused by: org.dom4j.DocumentException: www.apple.com Nested exception: www.apple.com at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.dom4j.io.SAXReader.read(SAXReader.java:291) ... 10 more 
+4
source share
3 answers

Your object recognizer does not process anything (since it always returns null). Make it return the InputSource to the actual DTD file if the system ID is http://www.apple.com/DTDs/PropertyList-1.0.dtd , since the DTD that is trying to load the dom4j file.

 public class MyResolver implements EntityResolver { public InputSource resolveEntity (String publicId, String systemId) { if (systemId.equals("http://www.apple.com/DTDs/PropertyList-1.0.dtd")) { return new InputSource(MyResolver.class.getResourceAsStream("/dtds/PropertyList-1.0.dtd"); } else { // use the default behaviour return null; } } } 

This implementation, for example, returns a DTD from the class path (in the dtds package). You just need to download the DTD yourself and link it in your application in the dtds package.

+5
source

Note that you are not actually testing DTDs. To do this, you need to do:

 SAXReader saxReader = new SAXReader(true); 

Otherwise, JB is on the right - he stopped 3 minutes before me!

0
source

Alternatively, if you just want to use SAXReader offline, disable its external DTD selection using the http://apache.org/xml/features/nonvalidating/load-external-dtd Xerces feature.

According to the Xerces Function Documentation , setting this parameter to false makes SAXReader completely ignore the external DTD.

This SO answer contains sample code.

0
source

Source: https://habr.com/ru/post/1390595/


All Articles