1

我知道已经回答了类似的问题,但它们并不能完全解决我的具体问题:

public String getValue(String xPath) {
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(new PersonalNamespaceContext());
    String value = null;
    NodeList nodes = null;
    try {
        XPathExpression expr = xpath.compile(xPath);
        nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
        System.out.println("nodes: " + nodes.getLength());
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (int idx = 0; idx < nodes.getLength(); idx++) {
        value = nodes.item(idx).getTextContent();
        System.out.println(value);
        //break;
    }
    return value;
}

我需要能够获得“可见”文本节点的值。

                 <itemlocation>
                    <x>0</x>
                    <y>0</y>
                 </itemlocation>
                 <visible>off</visible>
              </pane>

我似乎找不到正确的 xPath 值来执行此操作。有人有想法吗?

4

0 回答 0