Learn XQuery |
Home:Learn XQuery:XQuery Tutorial:An XQJ Tutorial:Processing Query Results XQJ Tutorial Part IV: Processing Query ResultsXQJ Tutorial Part III: Querying Data from XML Files or Java XML APIs describes how to execute queries. In XQuery, query evaluation results in a sequence. In XQJ, executing a query through XQExpression or XQPreparedExpression returns an XQSequence object. An XQSequence represents an XQuery sequence and a cursor to iterate over the sequence. Applications can navigate through an XQSequence using the next() method. Initially the current position of the XQSequence is before the first item. next() moves the current position forward and returns true if there is another item to be consumed. Once all items in the sequence have been read, next() returns false. Let's iterate through a sequence: ... Retrieving Data Using Get MethodsOnce positioned on an item, applications can retrieve the data using one of the getXXX() methods. Let’s take a look at some of these methods. An application can use getObject() to retrieve the current item of an XQSequence as a Java object. XQJ defines a mapping for each of the XQuery item types to a Java object value. Working with ElementsOne of the most common scenarios is probably a query returning a sequence of elements. Using getObject(), XQJ defines a mapping to Java DOM elements: ... Working with Atomic ValuesActually, XQJ defines a mapping for every XQuery type to Java objects, including all the atomic types. Assume, for example, a query returning xs:decimal values; using getObject() your Java application retrieves the items as java.math.BigDecimal objects: ... Suppose you have a query returning atomic values, and you want to retrieve a textual (character string) representation to output to System.out. The getAtomicValue() method returns a character string representation of an atomic value according to the XQuery xs:string casting rules, and throws an exception if the item is not an atomic value (that is, if it’s a node). In the next example the query returns a sequence of atomic values. Note that the items are not all of the same type: ... Working with SAX and StAXBeside the DOM, XQJ also provides native support for two other popular XML APIs — SAX and StAX. In the next example, each of the items is returned to the application through SAX: ... Reading a Sequence as a StreamSo far we have seen a number of examples in which the application iterates over all the items in the sequence, and retrieves them one-by-one. The XQSequence interface also offers functionality to retrieve the complete sequence within a single call. The next example executes a query and serializes the complete result into a SAX event stream. ... Similarly, the following example reads the complete sequence as a StAX event stream: ... Besides exposing the sequence through a SAX or StAX event stream, XQSequence also provides the ability to serialize into a binary or character stream. Here we're entering the arena of XSLT 2.0 and XQuery 1.0 Serialization, which is discussed in XQJ Tutorial Part V: Serializing Query Requests. Scrollable SequencesThe above examples all iterate forward through the XQSequence objects. XQJ has also the notion of scrollable sequences, allowing you to move both forward and backward through a sequence, set the cursor to an absolute position, and iterate through the XQSequence more than once. |
New Case StudyGevity produces sales proposals in real time using DataDirect XQuery®. See how Gevity uses DataDirect XQuery® to combine Web service data from SalesForce.com with relational data in Oracle in a pricing engine for HR management. New Features in DataDirect XQuery®DataDirect XQuery® is now released! DataDirect XQuery® provides full update support for relational data, easy integration for Web Services, additional enhancements for performance and scalability and more! DataDirect XQuery FAQThis informative DataDirect XQuery® FAQ answers frequently-asked questions about DataDirect XQuery®, including questions about performance, scalability, use-cases, resources, and more. If you're more of a hands-on learner, then download a free copy and start exploring DataDirect XQuery® today! |





