为什么XPath不会从html中检索特定的类?

时间:2015-11-19 20:20:34

标签: java xml parsing dom xpath

以下是我要解析的示例html:

<div class="MyFirstClass">
    the content I am able to retrieve
</div>
<div class="MySecondClass MyThirdClass" id="my_id">
    the content I want to retrieve
</div>

检查Google Chrome上的元素,第二个div为div#my_id.MySecondClass.MyThirdClass

这是我用来解析的Java代码。虽然content1实际上是“我能够检索的内容”,但我将content2变为null而不是预期的“我想要检索的内容”。

XPathExpression expression1 = Xpath.compile("//DIV[contains(concat(' ',@class,' '),' MyFirstClass ')]");
String content1 = expression1.evaluate(document);
XPathExpression expression2 = Xpath.compile("//DIV[contains(concat(' ',@class,' '),' MySecondClass ')]");
String content2 = expression2.evaluate(document);

我已针对expressions2尝试了以下所有变体,但所有变体都content2null

XPathExpression expression2 = Xpath.compile("//DIV[contains(concat(' ',@class,' '),' MyThirdClass ')]");
XPathExpression expression2 = Xpath.compile("//DIV[contains(concat(' ',@class,' '),' MySecondClass MyThirdClass ')]");
XPathExpression expression2 = Xpath.compile("//DIV[contains(concat(' ',@class,' '),' MySecondClassMyThirdClass ')]");
XPathExpression expression2 = Xpath.compile("//DIV[contains(concat(' ',@class,' '),' MySecondClass.MyThirdClass ')]");
XPathExpression expression2 = Xpath.compile("//DIV[contains(concat(' ',@class,' '),' MySecondClass\\.MyThirdClass ')]");
XPathExpression expression2 = Xpath.compile("//div[contains(concat(' ',@class,' '),' MySecondClass\\.MyThirdClass ')]");

expression2应该是什么,以便content2包含预期的“我想要检索的内容”?

0 个答案:

没有答案