如何在SAXParserFactory上设置useCatalog功能?

时间:2018-12-03 22:22:00

标签: java xml sax

编辑:通过从build.gradle中删除xercesImpl jar来解决。

我正在尝试在SAXParserFactory实例上设置useCatalog功能。在测试中,我回来了

SAXNotRecognizedException: Feature 'http://javax.xml.XMLConstants/feature/useCatalog' is not recognized.

设置SAXParserFactory的useCatalog功能的正确方法是什么?

我还尝试在CATALOG_FILE上设置SAXParser属性,但是测试返回

SAXNotRecognizedException: Property 'javax.xml.catalog.files' is not recognized.

SAXParser上设置目录属性的正确方法是什么?

我的示例代码遵循为XMLConstants.USE_CATALOG提供的JavaDoc示例。

private static final String CATALOG_FILE = CatalogFeatures.Feature.FILES.getPropertyName();
private static final String CATALOG_PREFER = CatalogFeatures.Feature.PREFER.getPropertyName();

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setXIncludeAware(true);
spf.setFeature(XMLConstants.USE_CATALOG, true);
SAXParser parser = spf.newSAXParser();

if (catalogURIs.size() > 0) {
    List<URI> catalogUriList = new ArrayList<>();

    for (String uriString : catalogURIs) {
        catalogUriList.add(URI.create(uriString));
    }

    CatalogResolver resolver =
            CatalogManager.catalogResolver(CatalogFeatures.defaults(),
            catalogUriList.toArray(new URI[catalogUriList.size()]));
    parser.setProperty(CATALOG_FILE, resolver);
    parser.setProperty(CATALOG_PREFER, "system");
}

0 个答案:

没有答案