![]() |
The Java Developers Almanac 1.4 |
|
e1074. Finding Elements by Absolute Location in a DOM Document Using XPathXPath 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 eX 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 ( // 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
// e510 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>
e1076. Finding Elements by Content in a DOM Document Using XPath e1077. Finding Elements by Id in a DOM Document Using XPath e1078. Finding Elements by Attributes in a DOM Document Using XPath
© 2002 Addison-Wesley. |