Using the DOMXPath Class Example of Using the DOMXPath Class

5-2 Programming XML for Oracle WebLogic Server WebLogic Server provides a full implementation of the JAXR specification. Using the JAXR API, you can create JAXR clients that access a registry, or you can create a registry yourself. For more information on using the JAXR API, see the Java Web Services http:java.sun.comxmljaxrindex.jsp Web site.

5.2 Using the WebLogic XPath API

The WebLogic XPath API contains all of the classes required to perform XPath matching against a document represented as a DOM, an XMLInputStream, or an XMLOutputStream. Use the API if you want to identify a subset of XML elements within an XML document that conform to a given pattern. For additional API reference information about the WebLogic XPath API, see weblogic.xml.xpath in the Oracle WebLogic Server API Reference and http:java.sun.comxmljaxrindex.jsp Javadoc.

5.2.1 Using the DOMXPath Class

This section describes how to use the DOMXPath class of the WebLogic XPath API to perform XPath matching against an XML document represented as a DOM. The section first provides an example and then a description of the main steps used in the example.

5.2.2 Example of Using the DOMXPath Class

The sample Java program at the end of this section uses the following XML document in its matching: ?xml version=1.0 encoding=us-ascii? -- Purchaseorder. -- purchaseorder department=Sales date=01-11-2001 raisedby=Pikachu item ID=101 titleLaptoptitle quantity5quantity makeDellmake item item ID=102 titleDesktoptitle quantity15quantity makeDellmake item item ID=103 titleOffice Softwaretitle quantity10quantity makeMicrosoftmake item purchaseorder Using Advanced XML APIs 5-3 The Java code example is as follows: package examples.xml.xpath; import java.io.IOException; import java.util.Iterator; import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; import weblogic.xml.xpath.DOMXPath; import weblogic.xml.xpath.XPathException; This class provides a simple example of how to use the DOMXPath API. author Copyright c 2002 by BEA Systems, Inc. All Rights Reserved. public abstract class DOMXPathExample { public static void mainString[] ignored throws XPathException, ParserConfigurationException, SAXException, IOException { create a dom representation of the document String file = purchaseorder.xml; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance; factory.setNamespaceAwaretrue; doesnt matter for this example DocumentBuilder builder = factory.newDocumentBuilder; Document doc = builder.parsefile; create some instances of DOMXPath to evaluate against the document. DOMXPath totalItems = count number of items new DOMXPathcountpurchaseorderitem; DOMXPath atLeast10 = titles of items with quantity = 10 new DOMXPathpurchaseorderitem[quantity = 10]title; evaluate them against the document double count = totalItems.evaluateAsNumberdoc; Set nodeset = atLeast10.evaluateAsNodesetdoc; 5-4 Programming XML for Oracle WebLogic Server output results System.out.printlnfile+ contains +count+ total items.; System.out.printlnThe following items have quantity = 10:; if nodeset = null { Iterator i = nodeset.iterator; whilei.hasNext { Node node = Nodei.next; System.out.println +node.getNodeName+ : +node.getFirstChild.getNodeValue; } } note that at this point we are free to continue using evaluate atLeast10 and totalItems against other documents } }

5.2.3 Main Steps When Using the DOMXPath Class