我试图用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/

10-10 14:31