Xpath Java通过测试值获取元素

时间:2014-03-18 23:55:49

标签: java xml xpath expression

我尝试使用Xpath解析xml字符串,但我不知道如何使用表达式来检索我想要的内容。

我有以下xml内容:

<root1>
    <root2>
        <A>something</A>
        <B>something</B>
        <C>
            <D>
                <E>DataE</E>
                <F>DataF</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                      <K>DataK</K>
                      <L>DataL</L>
                    </J>
                </I>
            </D>
            <D>
                <E>DataE_ERROR</E>
                <F>DataF</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                      <K>DataK</K>
                      <L>DataL</L>
                    </J>
                </I>
            </D>
        </C>
     </root2>
</root1>

例如,当E的值是DataE时,我想得到F,K,L的值。

我正在使用这个Xpath,但我无法找到好的表达!

这是我的java代码:

public void getit()
{
    String xml = "The xml above";

    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    DocumentBuilder b = f.newDocumentBuilder();
    Document d = b.parse(new InputSource(new StringReader(xml)));
    d.getDocumentElement().normalize();

    // No idea ! how can i make it work by testing the value of E
    String expression = "//E/text()|//F/text()|//K/text()|//L/text()";

    XPath xPath = XPathFactory.newInstance().newXPath();
    Object result = xPath.compile(expression).evaluate(d, XPathConstants.NODESET);

    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue()); 
    }

}

1 个答案:

答案 0 :(得分:3)

这样我可以确保查询正常工作,我修改了示例XML,如下所示......

<root1>
    <root2>
        <A>something</A>
        <B>something</B>
        <C>
            <D>
                <E>DataE</E>
                <F>DataF 1</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                        <K>DataK 1</K>
                        <L>DataL 1</L>
                    </J>
                </I>
            </D>
            <D>
                <E>DataE_ERROR</E>
                <F>DataF 2</F>
                <G>something</G>
                <H>something</H>
                <I>
                    <J>
                        <K>DataK 2</K>
                        <L>DataL 2</L>
                    </J>
                </I>
            </D>
        </C>
    </root2>
</root1>

魔法在这里......

String expression = "//F[ancestor::D/E[text()='DataE']]|//K[ancestor::D/E[text()='DataE']]|//L[ancestor::D/E[tex

基本上,这是......

查找任何F节点,其祖先包含D,其子级为E,其文本等于DataE或...

现在,这很重要,您可以使用../查找父节点,但KL隐藏在子节点中,我不确定它们是否可能更深或更深,所以我选择了这种方法。

您可能需要稍微改进一下,但我认为D/E的关系非常重要。

通过这个(以及下面的例子),我能够生成以下输出......

Found DataF 1
Found DataK 1
Found DataL 1

可运行的例子:

public class TestXPath {

    public static void main(String[] args) {
        String xml = "The xml above";

        try {
            DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
            DocumentBuilder b = f.newDocumentBuilder();
            Document d = b.parse(new File("Values.xml"));
            d.getDocumentElement().normalize();

            String expression = "//F[ancestor::D/E[text()='DataE']]|//K[ancestor::D/E[text()='DataE']]|//L[ancestor::D/E[text()='DataE']]";
            XPath xPath = XPathFactory.newInstance().newXPath();
            Object result = xPath.compile(expression).evaluate(d, XPathConstants.NODESET);

            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {

                Node node = nodes.item(i);
                System.out.println("Found " + node.getTextContent());
            }
        } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | DOMException exp) {
            exp.printStackTrace();
        }
    }

}