撒克逊人解析缓慢

时间:2012-02-11 23:09:04

标签: java xml-parsing saxon

我试图用saxon解析一些xml来对它进行一些xpath查询但是遇到了两个问题:第一个是saxon在xhtml中构建一个非常短的文档非常长。 代码是这样的:

Processor processorInstance = new Processor(false);
    processorInstance.setConfigurationProperty(FeatureKeys.DTD_VALIDATION, false);


    XPathCompiler XPathCompilerInstance = processorInstance.newXPathCompiler();
    XPathCompilerInstance.setBackwardsCompatible(false);

    String expressionTitre = "//div[@class='score_global']/preceding-sibling::img[1]";

    XPathExecutable XPathExecutableInstance = XPathCompilerInstance.compile(expressionTitre);
    XPathSelector selector = XPathExecutableInstance.load();
    logger.info("Xpath compiled.");

    // Phase 2, load xml document.
    DocumentBuilder documentBuilderInstance = processorInstance.newDocumentBuilder();
    documentBuilderInstance.setSchemaValidator(null);
    documentBuilderInstance.setLineNumbering(false);
    documentBuilderInstance.setRetainPSVI(false);


    XdmNode context = documentBuilderInstance.build(new File("sample/sample.xml")); // This line takes ages to return.

我不明白的是,如果我使用SAX,它会以正常速度加载:(。 我忘了在撒克逊人中提供什么?

Java 1.6 撒克逊人9.1.0.8

第二个问题是,当我的xml是这样的时候,他无法处理重音字符:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

所以我删除了xml:lang en lang = attributes但没有更好的运气:(

你有什么想法吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

好了经过多次阅读,只需要定义一个CatalogResolver并在本地下载Xhtml dtds。我放弃了撒克逊并改为使用简单的JaxP / SaxReader。

此页http://xml.apache.org/commons/components/resolver/resolver-article.html非常有趣。

希望这些考虑证明对某人有用:)

答案 1 :(得分:1)

好吧,我发现虽然我配置了Saxon不进行验证,但他仍尝试解析URI并且无法在本地找到它,因此他上网并得到了&amp;来自W3c的503需要很长时间才能返回。 我删除了我的xml中的DTD声明,它工作正常。 我的下一步是让它停下来尝试解决它。我目前正在阅读撒克逊文档并使用实体解析器,它应该没问题。