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