Navigating the Document Via XPath in Java

Another important method for navigating XML documents is via XPath. XPath is a non-XML based specification for addressing arbitrary locations in XML Documents. The XPath syntax is described in detail in Chapter 11, “Locating Components in XML Documents with XPath.” A basic usage of XPath with Java is provided in this section.

Unfortunately, a standard interface for XPath is not yet part of the standard Java API for XML provided by Sun. However, a robust implementation available for XPath works seamlessly with implementations of the Java API for XML like Xerces and Xalan. That XPath implementation is called Jaxen (available at http://jaxen.org).

Jaxen is a native Java implementation of the full XPath Specification and is capable of working with multiple Java XML access methods. For this section, we will use the familiar DOM interface with Jaxen.

Note

Jaxen isn't part of the standard Java XML Pack; therefore, to use this example you will need to download the Jaxen implementation from http://jaxen.org and install it. The only installation that is currently required is to add the “jaxen-full.jar” and saxpath.jar file from the Jaxen installation to the classpath for your IDE project (along with the standard xerces.jar and xalan.jar from the Java XML pack). However, be sure to read the installation documentation carefully to ensure correct functionality.


Using XPath via Jaxen is a straightforward process. First, because we will be using a DOM interface to work with the Jaxen engine, a DOM document is needed that represents the example XML document. Because Jaxen works seamlessly with the Java API For XML Processing, the process used to build this Document object is exactly the same as in the earlier section on DOM.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse( "example.xml" );

Now that the DOM document has been created, we're ready to execute the desired XPath expression. The API for this call is extremely simple. First, an XPath object is created with the XPath expression that we want to execute on the document. In this case, we will use the following XPath expression: "//standards/standard". This XPath expression will return all the XML elements named "standard" that are children of the "standards" element.

XPath xpath = new XPath( "//standards/standard" ); 

In order to run the XPath object against our document, the selectNodes method of the XPath object is called with the DOM document as the parameter. This allows the same XPath object to be executed with many different XML documents, which greatly increases efficiency.

List xpathResults = xpath.selectNodes(document); 

Now that the results have been generated, we need to access them. The process of accessing the results is a little different than in the Java API for XML, which uses the DOM object model to return lists of results. Here, the standard Java collection classes List and Iterator are used instead. The selectNodes method returns a Java List, which we can then iterate over with its Java Iterator:

Iterator resultsIterator = xpathResults.iterator(); 
while ( resultsIterator.hasNext() )
{
       Element resultElement = (Element) resultsIterator.next();
       System.out.println(resultElement.getTagName());
       System.out.println(resultElement.getAttribute("href"));
}

Each of the results contained in the Java List is a DOM Node. In this case, because our XPath expression queries for XML Elements, we know ahead of time that Elements will be returned. So each returned result is converted to a DOM Element and then the name of the <standard> element and the URL href attribute that it contains is printed out to the system console.

The full implementation of code for this example is contained in Listing 16.8, and the console output generated by the Jaxen XPath example is provided in Listing 16.9.

Listing 16.8. Java XPath Example
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import org.jaxen.dom.XPath;
import org.saxpath.SAXPathException;
import org.saxpath.XPathSyntaxException;
import javax.xml.parsers.ParserConfigurationException;
import java.util.List;
import java.util.Iterator;

public class SimpleXpath
{
  public static void main(String[] args)
 {
   try
  {
    // Generate a Document Builder Factory that will allow
    // DOM Parsers to be generated
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    // Set the factory to respect XML Namespaces
    factory.setNamespaceAware(true);
    // Using the Document Builder Factory, generate a DOM Parser
    DocumentBuilder builder = factory.newDocumentBuilder();

    // Using the generated DOM parser, parse the example file      
    Document document = builder.parse( "example.xml" );
          
    // Build an XPath object with the desired expression
    XPath xpath = new XPath( "//standards/standard" );

    // Run the XPath expression against the current document, returning a
    // list of matched elements
    List xpathResults = xpath.selectNodes(document);

    // Generate a Java iteratory that will allow the returned elements
    // to be traversed
    Iterator resultsIterator = xpathResults.iterator();
          
    System.out.println("Results:" );
          
    // Loop over each element, while there are elements remaining ,
    // printing out the values of each. 
     while ( resultsIterator.hasNext() )
     {
       Element resultElement = (Element) resultsIterator.next();
       System.out.println(resultElement.getTagName());
       System.out.println(resultElement.getAttribute("href"));
     }
   }
   catch (XPathSyntaxException e)
   {
     System.err.println(e);
   }

   catch (ParserConfigurationException e)
   {
     System.err.println(e);
   }
   catch (SAXException e)
   {
     System.err.println(e);
   }
   catch (IOException e)
   {
     System.err.println(e);
    }
  }
}

Note

This example depends on several Java libraries. If you have any problems compiling and running it, make sure that your classpath contains references to the standard Java XML Pack libraries (currently xerces.jar and xalan.jar), as well as references to the Jaxen libraries (currently jaxen-full.jar and saxpath.jar). In addition, make sure that you read the installation documentation to ensure correct installation.


Listing 16.9. Java XPath Example Output
Results:
standard
http://java.sun.com/xml/jaxp
standard
http://www.w3.org/DOM/
standard
http://www.saxproject.org/
					

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.225