Example of Using the StreamXPath Class

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

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? -- Stock Quotes. -- stock_quotes stock_quote symbol=BEAS when date01272001date time3:40PMtime when price type=ask value=65.1875 price type=open value=64.00 price type=dayhigh value=66.6875 price type=daylow value=64.75 change+2.1875change volume7050200volume stock_quote stock_quote symbol=MSFT 5-6 Programming XML for Oracle WebLogic Server when date01272001date time3:40PMtime when price type=ask value=55.6875 price type=open value=50.25 price type=dayhigh value=56 price type=daylow value=52.9375 change+5.25change volume64282200volume stock_quote stock_quotes The Java code for the example is as follows: package examples.xml.xpath; import java.io.File; import weblogic.xml.stream.Attribute; import weblogic.xml.stream.StartElement; import weblogic.xml.stream.XMLEvent; import weblogic.xml.stream.XMLInputStream; import weblogic.xml.stream.XMLInputStreamFactory; import weblogic.xml.stream.XMLStreamException; import weblogic.xml.xpath.StreamXPath; import weblogic.xml.xpath.XPathException; import weblogic.xml.xpath.XPathStreamFactory; import weblogic.xml.xpath.XPathStreamObserver; This class provides a simple example of how to use the StreamXPath API. author Copyright c 2002 by BEA Systems, Inc. All Rights Reserved. public abstract class StreamXPathExample { public static void mainString[] ignored throws XPathException, XMLStreamException { Create instances of StreamXPath for two xpaths we want to match against this tream. StreamXPath symbols = new StreamXPathstock_quotesstock_quote; StreamXPath openingPrices = new StreamXPathstock_quotesstock_quoteprice[type=open]; Create an XPathStreamFactory. XPathStreamFactory factory = new XPathStreamFactory; Create and install two XPathStreamObservers. In this case, we just use to anonymous classes to print a message when a callback is received. Note that a given observer can observe more than one xpath, and a given xpath can be observed by mutliple observers. factory.installsymbols, new XPathStreamObserver { public void observeXMLEvent event { System.out.printlnMatched a quote: +event; Using Advanced XML APIs 5-7 } public void observeAttributeStartElement e, Attribute a {} ignore public void observeNamespaceStartElement e, Attribute a {} ignore }; Note that we get matches for both a start and an end elements, even in the case of price which is an empty element - this is the behavior of the underlying streaming parser. factory.installopeningPrices, new XPathStreamObserver { public void observeXMLEvent event { System.out.printlnMatched an opening price: +event; } public void observeAttributeStartElement e, Attribute a {} ignore public void observeNamespaceStartElement e, Attribute a {} ignore }; get an XMLInputStream on the document String file = stocks.xml; XMLInputStream sourceStream = XMLInputStreamFactory.newInstance. newInputStreamnew Filefile; use the factory to create an XMLInputStream that will do xpath matching against the source stream XMLInputStream matchingStream = factory.createStreamsourceStream; now iterate through the stream System.out.printlnMatching against xml stream from +file; whilematchingStream.hasNext { we dont do anything with the events in our example - the XPathStreamObserver instances that we installed in the factory will get callbacks for appropriate events XMLEvent event = matchingStream.next; } } }

5.2.6 Main Steps When Using the StreamXPath Class