Laurent
Laurent

Reputation: 1749

ParserConfigurationException with javax.xml.parsers on Android

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

Upvotes: 0

Views: 3391

Answers (2)

Laurent
Laurent

Reputation: 1749

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

Upvotes: 0

lele chu
lele chu

Reputation: 11

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.

Upvotes: 1

Related Questions