Main Steps When Using the DOMXPath Class Using the StreamXPath Class

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

The following procedure describes the main steps to use the DOMXPath class to perform XPath matching against an XML document represented as a DOM: 1. Create an org.w3c.dom.Document object from an XML document, as shown in the following code excerpt: String file = purchaseorder.xml; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance; DocumentBuilder builder = factory.newDocumentBuilder; Document doc = builder.parsefile; 2. Create a DOMXPath object to represent the XPath expression you want to evaluate against the DOM. The following example shows an XPath expression that counts the items in a purchase order: DOMXPath totalItems = new DOMXPathcountpurchaseorderitem; The following example shows an XPath expression that returns the titles of items whose quantity is greater or equal to 10: DOMXPath atLeast10 = new DOMXPathpurchaseorderitem[quantity = 10]title; Using Advanced XML APIs 5-5 3. Evaluate the XPath expression using one of the DOMXPath.evaluateAsXXX methods, where XXX refers to the data type of the returned data, such as Boolean, Nodeset, Number, or String. The following example shows how to use the evaluateAsNumber method to evaluate the totalItems XPath expression: double count = totalItems.evaluateAsNumberdoc; System.out.printlnfile+ contains +count+ total items.; The following example shows how to use the evaluateAsNodeset method to return a Set of org.w3c.dom.Nodes which you can iterate through in the standard way: Set nodeset = atLeast10.evaluateAsNodesetdoc; 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; } } For additional API reference information about the WebLogic XPath API, see weblogic.xml.xpath in the Oracle WebLogic Server API Reference.

5.2.4 Using the StreamXPath Class

The example in this section shows how to use the StreamXPath class of the WebLogic XPath API to perform XPath matching against an XMLInputStream. The section first provides an example and then a description of the main steps used in the example. Although the example shows how to match only against an XMLInputStream, you can use similar code to match against an XMLOutputStream.

5.2.5 Example of Using the StreamXPath Class