Streaming API for XML (StAX) is an API
to process XML documents. As official documents of StAX
http://jcp.org/en/jsr/detail?id=173
).
The primary goal of the StAX API is to give "parsing control to
the programmer by exposing a simple iterator based API. This allows
the programmer to ask for the next event (pull the event) and allows
state to be stored in procedural fashion." StAX was created to
address limitations in the two most prevalent parsing APIs, SAX and
DOM.
--StAX is a pull API
--StAX has bi directional support means
it can do both XML reading and writing .
Streaming Vs DOM
There are two models of programming to
work with XML infosets : document streaming and document object model
(DOM)
In DOM based processing , in memory
objects are created to represent entire document as a trree while
processing the xml document. Obviously there are memory and
processing issues involved with these . If XML document is smaller
then we can ignore these issue but if document getting larger then we
need to re think about DOM based solution. Though DOM based API gives
developer programmatic flexibility but gives some memory and
processing overhead.
On the other hand streaming refer to a
programming model where xml document are processd serially and in
application run time and often from dynamic sources whose contents
are not precisely known beforehand. Moreover, stream-based parsers
can start generating output immediately, and infoset elements can be
discarded and garbage collected immediately after they are used.
While providing a smaller memory footprint, reduced processor
requirements, and higher performance in certain situations, the
primary trade-off with stream processing is that you can only see the
infoset state at one location at a time in the document. You are
essentially limited to the "cardboard tube" view of a
document, the implication being that you need to know what processing
you want to do before reading the XML document.
Streaming models for XML processing are
particularly useful when your application has strict memory
limitations, as with a cellphone running J2ME, or when your
application needs to simultaneously process several requests, as with
an application server. In fact, it can be argued that the majority of
XML business logic can benefit from stream processing, and does not
require the in-memory maintenance of entire DOM trees.
Pull Vs Push Style API
In case of pull type programming model
application need explicit call to read data from xml info-set means
pulling from xml infoset .
But, in case of push parsing model
parser API push the data to application when encounter any element in
xml infoset.
Pull Parsing Has Some Advantages Over
Push Model :
---client has control on application
thread
--lees code writing
--can handle multiple documents at ones
in a single thread
--StAX parser has filtering
capabilities to ignore unwanted elements in xml infoset
--support xml views of non xml data
Reference
http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/tutorial/doc/SJSXP2.htmlSample StAX Code To Write XML Document
public void generateXML () { String generatedXMLAsString = ""; try { XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(byteArrayOutputStream); xmlStreamWriter.writeStartDocument(); xmlStreamWriter.writeCharacters("\n\n"); xmlStreamWriter.writeStartElement("rootElement"); xmlStreamWriter.writeNamespace("", "http://wwww.example.com/hello"); xmlStreamWriter.writeCharacters("\n"); xmlStreamWriter.writeStartElement("childElement"); xmlStreamWriter.writeCharacters("hello"); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeCharacters("\n"); xmlStreamWriter.writeEndElement(); xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); generatedXMLAsString = new String(byteArrayOutputStream.toString()); System.out.println(generatedXMLAsString); byteArrayOutputStream.close(); } catch (Exception e) { System.out.println(e.getMessage()); } }
Generated XML
hello
No comments:
Post a Comment