0

I'm trying to use javax.xml.parsers on Android but I always get a ParserConfigurationException when trying to set these two features :

factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);

Here is my code

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    try {
            factory.setFeature("http://xml.org/sax/features/namespaces", false);
            factory.setFeature("http://xml.org/sax/features/validation", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);

    } catch (ParserConfigurationException e) {
            e.printStackTrace();
    }
Laurent
  • 1,661
  • 16
  • 29

2 Answers2

1

Feature names are fully qualified URIs. Implementations may define their own features. A ParserConfigurationException is thrown if this DocumentBuilderFactory or the DocumentBuilders it creates cannot support the feature. It is possible for a DocumentBuilderFactory to expose a feature value but be unable to change its state.

lele chu
  • 11
  • 3
  • Yes, I read the docs on https://developer.android.com/reference/javax/xml/parsers/DocumentBuilderFactory.html too. So it means these features are not supported in Android SDK for now I guess... – Laurent Jan 06 '17 at 07:56
0

The docs on developer.android.com says

Feature names are fully qualified URIs. Implementations may define their own features. An ParserConfigurationException is thrown if this DocumentBuilderFactory or the DocumentBuilders it creates cannot support the feature. It is possible for an DocumentBuilderFactory to expose a feature value but be unable to change its state.

But it looks this feature exists in xerces.apache.org

So I guess it means these features (used for document validation) are not supported in Android SDK for now.

Just for information. I found these error using an Epub parser library available in Android Arsenal EpubParser. I'm not the only one to have find this issue. Looks like there is a problem with this library because these two unsupported features are used regarding the code :

factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);

Laurent
  • 1,661
  • 16
  • 29