Comparing Java XML Parsers Ver1 0
Comparing Java XML Parsers Ver1 0
PRESENTED BY
The in-memory storage and management of data is a key part of any programming language and
Within a single application, the programmer is free to decide how the data is stored and represented.
Problem - In either case, there tends to be a lack of flexibility in the data representation,
causing problems when versions change or when data needs to be exchanged between disparate applications, frequently from different vendors.
was developed to address these issues. XML is written in plain text, uses self-describing elements and provides a data encoding format that is: Generic Simple Flexible Extensible Portable
XML offers a method of putting structured data in a text file. Structured data conforms to a particular
format; examples are spreadsheets, address books, configuration parameters, and financial transactions.
This plain text data provides software- and hardware-independent way of storing data making it easier
Exchanging data as XML greatly reduces this complexity, since the data can be read by different While upgrading to a new systems large volume of data must be converted and incompatible data is
often lost. XML plain text format. This makes it easier to expand or upgrade to new systems, without losing data.
With XML, data can be available to all kinds of "reading machines" (Handheld computers, voice
The following is a valid, well-formatted XML file <?xml version="1.0"?> <!-- This is a comment --> <address> <name>Lars </name> <street> Test </street> <telephone number= "0123"/> </address>
Defines a mechanism for accessing and manipulating well-formed XML. Using the DOM API, the XML document is transformed into a tree structure in memory. The application then navigates the tree to parse the document. If the document is large, it can place a strain on system resources. Defines XML parsing methods. Event based parser, the SAX parser streams a series of events while it reads the document. These events are forwarded to event handlers, which also provide access to the data of the document. Consumes extremely low memory, XML is not required to be loaded into the memory at one time. Need to implement all the event handlers to handle each and every incoming event. Incapable of processing the events when it comes to the DOM's element supports, and need to keep track of the parsers position in the document hierarchy. The application logic gets tougher as the document gets complicated and bigger.
It may not be required that the entire document be loaded but a SAX parser still requires to parse the whole document, similar to the DOM.
It lacks a built-in document support for navigation like the one which is provided by XPath. Along with the existing problem the one-pass parsing syndrome also limits the random access support.
StAX was designed as a median between these two opposites. The programmatic entry point is a cursor that represents a point within the document. The application moves the cursor forward - 'pulling' the information from the parser as it needs.
Why StAX ?
StAX shares with SAX the ability to read arbitrarily large documents. However, in StAX the application is in control rather than the parser. The application tells the parser when it wants to receive the next data chunk rather than the parser StAX exceeds SAX by allowing programs to both read existing XML documents and create new ones. Unlike SAX, StAX is a bidirectional API.
This interface represents a cursor that's moved across an XML document from beginning to end. At any given time, this cursor points at one event: text node, start-tag, comment, etc. The cursor always moves forward, never backward, and normally only moves one item at a time. Methods like getName and getText can be invoked to retrieve information. A typical StAX program begins by using the XMLInputFactory class to load an implementation dependent instance of XMLStreamReader.
InputStream in = new FileInputStream(new File("src/com/xmlparsers/jaxb/xsd/CIMMarketerProfile.xml")); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader staxParser = factory.createXMLStreamReader(in);
Expression
nodename
/ // . ..
Description
Selects all child nodes of the named node
Selects from root node Selects nodes from the current node that match the selection no matter where they are Selects the current node Selects the parent of the current node
@
* @* node()
Selects attributes
Matches any element node Matches any attribute nodes Matches any node of any kind