ParserConfigurationException with javax.xml.parsers on Android

3.1k views Asked by At

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();
    }
2

There are 2 answers

0
Laurent On BEST ANSWER

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);

1
lele chu On

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.