1

I'm trying to use Xpath to parse an xml string, but i don't know how use the expression to retrieve what i want.

I have the following xml content :

<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>

I would like to get the value of F,K,L when the value of E is DataE for example.

I'm using for this Xpath but i can't find the good expression !

here is my java code :

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 Answer 1

3

Just so I could ensure the query was working, I modified the example XML as follows...

<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>

The magic is here...

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

Basically, this reads...

Find any F node, who has an ancestor that contains D with a child of E whose text is equal to DataE or ...

Now, this is important, you could use ../ to find the parent node, but K and L are buried in sub nodes and I'm not sure if they might be deeper or shallow, so I went for this method.

You might need to refine it a bit, but I took the relationship of D/E as been important.

With this (and the example below), I was able to generate the following output...

Found DataF 1
Found DataK 1
Found DataL 1

Runnable example:

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();
        }
    }

}
10
  • This is just awsome, thank you, if i would like with example adding another relation in between J and K (show J only if K = dataK) how can i do it ?
    – zeomega
    Mar 19, 2014 at 1:19
  • Start by making that query work...I suspect something like //J[ancestor::K[text()='DataK']] or something might do it... Mar 19, 2014 at 1:35
  • Sorry .. i mean a relation between K and L. (show L only if K = dataK) I tried this : //F[ancestor::D/E[text()='DataE']]|//L[ancestor::J/K[text()='DataK']] .. but missing the "E" validation
    – zeomega
    Mar 19, 2014 at 1:51
  • 1
    The best I can do is give you the parent D node, //D[E[text()='DataE'] and descendant::K[text()='DataK']], you could then use this and another xPath query to find the children... Mar 19, 2014 at 2:35
  • 1
    Or you could try //F[ancestor::D/E[text()='DataE'] and ancestor::D/descendant::K[text()='DataK']]|//L[ancestor::D/E[text()='DataE'] and ancestor::D/descendant::K[text()='DataK']] - that's a mouth full :P Mar 19, 2014 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.