Finding Elements by Absolute Location in a DOM Document Using XPath

XPath is an expression language for selecting nodes in an XML file. For example, the XPath expression /a/b selects all b elements under the root element a. This example demonstrates some common XPath expressions for selecting elements based on the document root. For examples of XPath expressions relative to a particular node, see X. For more information about XPath, see the specification at http://www.w3c.org/TR/xpath

The XPath package used in this example selects nodes in a DOM document. Hence, the XML file is first parsed into a DOM document and the XPath expression is then applied to the DOM document. The result of an XPath expression is shown next to the expression; the numbers are ids of elements in the sample file shown at the end of this example.

Note: This example uses the XPath packages (org.apache.xpath.*) available in J2SE 1.4. However, these packages are not yet part of the Java 2 API and so this example may not work in future versions of J2SE.

// Get the root element (without specifying its name)
String xpath = "/*";                       // 1

// Get the root element (using its name)
xpath = "/root";                           // 1

// Get all elements directly under the root
xpath = "/root/*";                         // 2 8 12

// Get all e elements directly under the root
xpath = "/root/e";                         // 12

// Get all e elements in the document
xpath = "//e";                             // 4 6 10 11 12

// Get all non-e elements in the document
xpath = "//*[name() != 'e']";              // 1 2 3 5 7 8 9

// Get all e elements directly under an elem1 element
xpath = "//elem1/e";                       // 10 11

// Get all e elements anywhere under an elem1 element
xpath = "//elem1//e";                      // 4 6 10 11

// Get all elements with at least one child element
xpath = "//*[*]";                          // 1 2 3 5 8

// Get all elements without a child element
xpath = "//*[not(*)]";                     // 4 6 7 9 10 11 12

// Get all elements with at least one child e element
xpath = "//*[e]";                          // 1 3 5 8

// Get all elements with more than one child e elements
xpath = "//*[count(e)>1]";                 // 8

// Get all non-e elements without an e child element
xpath = "//*[not(e) and name() != 'e']";   // 2 7 9

// Get all level-4 e elements (the root being at level 1)
xpath = "/*/*/*/e";                        // 4

// Get all elements with more than one child e elements
xpath = "//*[count(e)>1]";                 // 8
XPath 1.0 does not support regular expressions to match element names. However, it is possible to perform some very simple matches on element names.
// Get all elements whose name starts with el
xpath = "//*[starts-with(name(), 'el')]";  // 2 3 5 7 8 9

// Get all elements whose name contains with lem1
xpath = "//*[contains(name(), 'lem1')]";   // 2 8
Sets of elements can also be combined using the union operator |
// Get all e elements directly under either the root or an elem2 element
xpath = "/*/e | //elem2/e";                // 4 12
Elements are returned in document order, that is, if the location of A's start tag appears before the location of B's start tag in the XML document, element A is returned before element B. Moreover, the returned elements are unique.
// Read an XML document; this method is implemented in
// The Quintessential Program to Create a DOM Document from an XML File
Document doc = parseXmlFile("infilename.xml", false);

try {
    // Get the matching elements
    NodeList nodelist = org.apache.xpath.XPathAPI.selectNodeList(doc, xpath);

    // Process the elements in the nodelist
    for (int i=0; i<nodelist.getLength(); i++) {
        // Get element
        Element elem = (Element)nodelist.item(i);
    }
} catch (javax.xml.transform.TransformerException e) {
}
Here is the sample XML file used in the example:
<?xml version="1.0" encoding="UTF-8"?>
<root id="1">
    <elem1 id="2">
        <elem2 id="3">
            <e id="4"/>
            <elem3 id="5">
                <e id="6"/>
            </elem3>
            <elem3 id="7"/>
        </elem2>
    </elem1>
    <elem1 id="8">
        <elem2 id="9"/>
        <e id="10"/>
        <e id="11"/>
    </elem1>
    <e id="12"/>
</root>

Comments

30 Aug 2011 - 3:44am by bingoonlineitaliano.org (not verified)

Nice to be visiting your blog again, it has been months for me. I need this article to complete my assignment in college. Thanks.

12 Oct 2011 - 7:13am by Anonymous (not verified)

Really helpful-

17 Oct 2011 - 11:34pm by Yogesh (not verified)

Please anyone tell that how to find Xpath of Element Using C# (Not using XML )Using Webbrowser

26 Oct 2011 - 10:41pm by Jayachandran J (not verified)

i am trying to get part the XML from XML which is not well formed. is there any way to get it.

21 Nov 2011 - 7:39pm by Anonymous (not verified)

we are lalaloopsy

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.