O'Reilly logo

Core Java® Volume II—Advanced Features, Ninth Edition by Gary Cornell, Cay S. Horstmann

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.4. Locating Information with XPath

If you want to locate a specific piece of information in an XML document, 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:

1. Get the document node.

2. Enumerate its children.

3. Locate the database element.

4. Get its first child, the username element.

5. Get its first child, a text

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required