我org.jdom2.xpath
用来评估对文档的XPath
查询。html
尝试从head
元素中检索脚本文本,我尝试了以下查询:
/html/head/script[contains(text(), 'expression1') and contains(text(), 'expression2')]/text()
此查询在XPath Helper和Chrome 控制台($x
查询)中返回单个结果,但使用org.jdom2.xpath
.
尝试更简单(但更重)的查询:
//script[contains(text(), 'expression1') and contains(text(), 'expression2')]/text()
产生相同的结果。
代码示例:
String xpath = "/html/head/script[contains(text(), 'expression1') and contains(text(), 'expression2')]/text()";
List<Text> tokeScriptResults = (List<Text>) xpathFactory.compile(xpath).evaluate(document);
事后思考:查看Document
对象,我发现由于脚本文本很长,jdom2
因此将其拆分为Text
s 数组而不是一个 long Text
。这可能是问题吗?