| Start |
|---|
| Parsing a document using the StAX API Considering the following document : One can use the following code to parse it and build a map of book titles by book id. This outputs : In this sample, one must be carreful of a few things : For more complex documents parsing (deeper, nested elements, ...), it is a good practice to "delegate" the parser to sub-methods or other objets, e.g. have a One can also use a |
| Parsing and navigating a document using the DOM API Considering the following document : One can use the following code to build a DOM tree out of a The code yields the following : RemarksXML Parsing is the interpretation of XML documents in order to manipulate their content using sensible constructs, be they "nodes", "attributes", "documents", "namespaces", or events related to these constructs. Java has a native API for XML document handling, called JAXP, or Java API for XML Processing (opens new window). JAXP and a reference implementation has been bundled with every Java release since Java 1.4 (JAXP v1.1) and has evolved since. Java 8 shipped with JAXP version 1.6. The API provides different ways of interacting with XML documents, which are :
Principles of the DOM interfaceThe DOM interface aims to provide a W3C DOM (opens new window) compliant way of interpreting XML. Various versions of JAXP have supported various DOM Levels of specification (up to level 3). Under the Document Object Model interface, an XML document is represented as a tree, starting with the "Document Element". The base type of the API is the The DOM interface is very usefull since it allows a "one line" parsing of XML documents as trees, and allows easy modification of the constructed tree (node addition, suppression, copying, ...), and finally its serialization (back to disk) post modifications. This comes at a price, though : the tree resides in memory, therefore, DOM trees are not always practical for huge XML documents. Furthermore, the construction of the tree is not always the fastest way of dealing with XML content, especially if one is not interested in all parts of the XML document. Principles of the SAX interfaceThe SAX API is an event-oriented API to deal with XML documents. Under this model, the components of an XML documents are interpreted as events (e.g. "a tag has been opened", "a tag has been closed", "a text node has been encountered", "a comment has been encountered")... The SAX API uses a "push parsing" approach, where a SAX The SAX interface overcomes the DOM interface's limitations by keeping only the minimum necessary data at the parser level (e.g. namespaces contexts, validation state), therefore, only informations that are kept by the Principles of the StAX interfaceThe StAX API takes a similar approach to processing XML as the SAX API (that is, event driven), the only very significative difference being that StAX is a pull parser (where SAX was a push parser). In SAX, the The API starts with XMLStreamReader (opens new window) (or XMLEventReader (opens new window)), which are the gateways through which the developer can ask |
Read Article
https://develop.horje.com/learn/1434/reference