Java Reference
In-Depth Information
Selecting Values from an XML Document
Problem
After parsing, you need to select only certain values from an XML document to work with.
Solution
Use Java's built-in XPath API to search for elements and attributes that match your criteria.
While the language in previous versions provided for the execution of XPath operations, the
updated XPath API was added in Java 5. The libraries are now much easier to work with and
are more stable.
XPath is important for SOA work because it is useful in two key areas: searching the payload
of SOAP messages for certain values when you're working with the SAAJ API in a server-
side provider and working with BPEL assignments in service orchestrations. We'll discuss
more in later chapters; for now it is just important to have a good handle on the kinds of things
you can do with XPath.
The program listing in Example 3-5 shows how you can parse an XML document and compile
a variety of XPath expressions to find different values. Here you will use the Catalog.xmlfile
introduced earlier in this chapter.
Example3-5.BasicXPath.java
package com.sc.ch02.xpath;
import static java.lang.System.out;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
/**
* Accepts an XPath expression to perform searching against
* the Catalog.xml document.
*/
Search WWH ::




Custom Search