Locating Information with XPath
If you want to locate a specific piece of information in an XML document, then it can be a bit of a hassle to navigate the nodes of the DOM tree. The XPath language makes it simple to access tree nodes. For example, suppose you have this XML document:
<configuration> . . . <database> <username>dbuser</username> <password>secret</password> . . . </database> </configuration>
You can get the database user name by evaluating the XPath expression
/configuration/database/username
That's a lot simpler than the plain DOM approach:
Get the document node.
Enumerate its children.
Locate the database element.
Get its first child, the username element.
Get its first child, a Text node.
Get its data.
An XPath can describe a set ...
Get Core Java™ 2 Volume II - Advanced Features, Seventh Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.