Thread: What is STAX?
View Single Post
  #4 (permalink)  
Old 10-13-2007, 01:35 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Smile Re: What is STAX?

It would be streaming in the sense that it would be very lazy and not read things in until needed. It would also be streaming in the sense that it would read everything forwards (but not backwards).

Here's what code that used such an API would look like.

Code:
URL url = ...
XMLStream xml = XXXFactory(url.inputStream()) ;


// process each <book> element in this document.
// the <book> element may have subnodes.
// You get a DOM/JDOM like tree rooted at the next <book>.


while (xml.hasContent()) {
XMLElement book = xml.getNextElement("book");
processBook(book);
}
Another variation would be:
Note that the implementation of the container would be lazy. I.e. it would only read things as they are pulled by the container.

Code:
Collection<XMLElement> books = xml.getAllElement("book");
for (XMLElement book : books) {
processBook(book);
}
There would also be XPath aware versions of the above.

Code:
Collection<XMLElement> books = xml.getAllElement("/*/libraries[city='chicago']/book");
for (XMLElement book : books) {
processBook(book);
}

And methods for controlling the depth of the produced tree. Something like (not sure of the best syntax).

This example would create a collection of books, but only retain the name, ISBN and author of each and ignore everything else.


Code:
Collection<XMLElement> books = xml.getAllElement("/*/libraries[city='chicago']/book",
restrict("name|ISBN|author"));
for (XMLElement book : books) {
processBook(book);
}
Such a system could be very memory efficient and easy to program with. It is what I thought Stax would be, before I saw the Stax examples.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Reply With Quote