简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM