以下是我要解析的示例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
尝试了以下所有变体,但所有变体都content2
为null
。
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
包含预期的“我想要检索的内容”?