Java API for XML Processing

The Java API for XML Processing, or JAXP, is one of the Java XML APIs. It is a lightweight standardized API for validating, parsing, generating and transforming XML documents. The respective ( non-standard ) implementation of the APIs is interchangeable ( pluggable ). The four basic interfaces are:

  • The Document Object Model parsing interface or DOM interface
  • The Simple API for XML parser interface or SAX interface
  • The Streaming API for XML or StAX interface (added in JDK 6; separately available as a jar for JDK 5)
  • XSLT interface to allow transformation to data structures of an XML document.

The J2SE 1.4 JDK JDK was the first version that was released with an implementation of JAXP 1.1, the current JSE ships with Apache Xerces and an adapted version of Xalan ( XSLT ).

DOM interface

The DOM interface is very simple. This method parses an entire XML document and creates a complete "in memory " representation of the document. It uses the classes and concepts of the specification, which is available at the Document Object Model (DOM ) Level 2 Core Specification.

The DOM parser Document Builder named because it provides an in -memory document representation. An instance of the class javax.xml.parsers.DocumentBuilder generated by the javax.xml.parsers.DocumentBuilderFactory factory class. The Document Builder, first create a org.w3c.dom.Document instance in the form of a tree structure that contains the node in the XML document. Each tree node in this structure implements the org.w3c.dom.Node interface. There are many different types of tree nodes that represent the respective types of data from the XML document.

The main nodes are:

  • Element node, possibly with attributes
  • Text nodes, which reflect the text found between the start and end tag of a document element

For a complete list of node types, refer to the Javadoc Documentation for package org.w3c.dom.

Using the DOM API, you can work ' in both directions ', ie from XML to "in memory " DOM and DOM from the XML. So it is not only suitable for " parse " XML but also to generate XML ( streams or files).

SAX interface

The SAX parser, also called SAXParser is created by javax.xml.parsers.SAXParserFactory. In contrast to the DOM parser SAXParser not created " in -memory" representation of an XML document, which makes it faster and therefore less demanding in memory consumption. In contrast, the SAXParser informed the client of the XML document structure by callback functions ( callbacks ), that is, there are methods of the DefaultHandler instance available to the parser available executed.

The default handler class is org.xml.sax.helpers in the package. This implements the ContentHandler, the error handler, the DTDHandler and the EntityResolver interface. Most clients are interested in the methods of the ContentHandler interface.

The ContentHandler methods implemented by the default handler will be called when the SAX parser encounters the corresponding elements of the XML document. The most important methods in this interface are:

  • The startDocument () and endDocument () method that will be called at the start and the end tag of an XML document.
  • The startElement () and endElement () method that will be called at the start and at the end date of a document element.
  • The characters ( ) method. This is called with the content, which is located between start and end date of each XML document element.

The client provides a subclass of the default handler that overrides these methods and processes the data. This may include storing the data in a database or writing to a stream include.

Using the SAX API can be ' in one direction ' work, from the XML 'in' in Java. So it is suited only for " parse " XML. With SAX so you can not generate XML ( streams or files).

XMLPULL interface

Since JAXP 1.2 and thus JSE 6 and J2EE 1.4 is the Streaming API for XML ( StAX ) is part of JAXP. This is used to read data using so-called XMLPULL parser XML. XMLPULL is similar to SAX, except that the parser does not like SAX sends information to the application via an event mechanism ( "PUSH "), but the application fetches the next information itself when it needs it ( " PULL "). XMLPULL parsers are usually more efficient than SAX parser.

XSLT interface

The Extensible Stylesheet Language for Transformations, abbreviated XSLT, allows the conversion of an XML document to other forms of data.

XSD validation

Validation of XSD files is supported by JAXP 1.2. JAXP 1.2 is from JSE 6.0 and J2EE 1.4 of the Java platform.

432279
de