java - 撒克逊人解析缓慢

标签 java xml-parsing saxon

我试图用 saxon 解析一些 xml 以对其进行一些 xpath 查询,但遇到了 2 个问题:第一个是 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= 属性,但没有更好的运气:(

你有什么想法吗? 谢谢!

最佳答案

好吧,经过大量阅读,只需要定义一个 CatalogResolver 并在本地下载 Xhtml dtds。我放弃了 saxon,改用简单的 JaxP/SaxReader。

本页http://xml.apache.org/commons/components/resolver/resolver-article.html事实证明非常有趣。

希望这些注意事项对某人有用:)

关于java - 撒克逊人解析缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9244862/

相关文章:

java - 在静态方法中使用 this.var 时出错,如果使用任何其他 var 名称,错误就会消失

java - 输出为 "NaN"

Java DOM XML 解析器 - 打印的 XML 包含一个标签而不是多个标签

撒克逊 XPath 错误 - "cannot serialize attribute"

具有可变数量源文件的 XSLT 工作流程

java - 在 Java 中使用 Double 值序列化和反序列化数据

使用凯撒密码对字符串进行 Java 装饰器模式

Java、SimpleXML 在一个路径中具有不同的前缀

php - 无法解析格式不正确的 XML

java - 如何区分从 Saxon XPathSelector 返回的属性节点和元素节点