Main Steps When Using the StreamXPath Class

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

The following procedure describes the main steps to use the StreamXPath class to perform XPath matching against an XML document represented as an XMLInputStream: 1. Create a StreamXPath object to represent the XPath expression you want to evaluate against the XMLInputStream. StreamXPath symbols = new StreamXPathstock_quotesstock_quote; The following example shows an XPath expression that matches stock quotes using their opening price: StreamXPath openingPrices = new StreamXPathstock_quotesstock_ quoteprice[type=open]; 2. Create an XPathStreamFactory. Use this factory class to specify the set of StreamXPath objects that you want to evaluate against an XMLInputStream and to create observers using the XPathStreamObserver interface used to register a callback whenever an XPath match occurs. The following example shows how to create the XPathStreamFactory: XPathStreamFactory factory = new XPathStreamFactory; 5-8 Programming XML for Oracle WebLogic Server 3. Create and install the observers using the XPathStreamFactory.install method, specifying the XPath expression with the first StreamXPath parameter, and an observer with the second XPathStreamObserver parameter. The following example shows how to use an anonymous class to implement the XPathStreamObserver interface. The implementation of the observe method simply prints a message when a callback is received. In the example, the observeAttribute and observeNamespace methods do nothing. factory.installsymbols, new XPathStreamObserver { public void observeXMLEvent event { System.out.printlnMatched a quote: +event; } public void observeAttributeStartElement e, Attribute a {} public void observeNamespaceStartElement e, Attribute a {} } ; 4. Create an XMLInputStream from an XML document: String file = stocks.xml; XMLInputStream sourceStream = XMLInputStreamFactory.newInstance.newInputStreamnew Filefile; 5. Use the createStream method of the XPathStreamFactory to create a new XMLInputStream that will perform XPath matching against the original XMLInputStream: XMLInputStream matchingStream = factory.createStreamsourceStream; 6. Iterate over the new XMLInputStream. During the iteration, if an XPath match occurs, the registered observer is notified: whilematchingStream.hasNext { XMLEvent event = matchingStream.next; } For additional API reference information about the WebLogic XPath API, see weblogic.xml.xpath in the Oracle WebLogic Server API Reference. 6 XML Programming Best Practices 6-1 6 XML Programming Best Practices The following sections discuss best programming practices when creating Java applications that process XML data: ■ Section 6.1, When to Use the DOM, SAX, and StAX APIs