获取包含HTML任务的节点文本

时间:2015-10-08 10:05:55

标签: java xpath

我尝试用Java XPath检查一个像这样的html字符串:

<app>
    <elem class="A">value1</elem>
    <elem class="B">value2a<br />value2b</elem>
    <elem class="C">value3</elem>
</app>

实际上,为了获得 elem 的值,我使用此代码

public String getValue(String xml, String classValue){
    XPath xpath = XPathFactory.newInstance().newXPath();
    InputSource source = new InputSource(new StringReader(xml));
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    document = db.parse(source);

    String xpathRequest = "//*[@class='"+classValue+"']/text()";
    String value = xpath.evaluate(xpathRequest , document);

    return value;
}

对于A类和C类工作正常,但当我问B类任务的内容时,只获得value2a

我如何获得完整的节点字符串?

2 个答案:

答案 0 :(得分:1)

简单地运行

String xpathRequest = "//*[@class='"+class+"']";
String value = this.xpath.evaluate(xpathRequest , document);

这将选择<elem>节点,当转换为String构建时,所有文本内容的串联,例如, Value2a Value2b

要获取Elem下面的所有文本内容的列表,您需要将它们选为NodeSet:

String xpathRequest = "//*[@class='"+class+"']/text()";
NodeList textNodes = (NodeList)xpath.evaluate(xpathRequest , document, XPathConstants.NODESET);
ArrayList<String> texts = new ArrayList<>();
for (int i=0; i<textNodes.getLength(); i++)
    texts.add(textNodes.item(i).getTextContent());

答案 1 :(得分:0)

这是因为此时xpath将返回2个值。请尝试以下方法: -

List<WebElement> allprice = driver.findElements(By.xpath("//*[@class='B']/text()"));
for(WebElement a:WebElement allprice){
   System.out.println(a.gettext());
}