Java API for XML Processing: Difference between revisions

Content deleted Content added
No edit summary
m rv blanking by 68.83.141.56
Line 1:
The '''Java API for [[XML]] Processing''', or '''JAXP''', is one of the [[Java XML]] [[programming]] [[API]]s. It provides the capability of validating and parsing [[XML]] documents. The two basic parsing interfaces are:
* the [[Document Object Model]] parsing interface or '''DOM''' interface
* the [[Simple API for XML]] parsing interface or '''SAX''' interface
 
In addition to the parsing interfaces, the API provides an XSLT interface to provide data and structural transformations on an XML document. JAXP was developed under the [[Java Community Process]] as JSR 5 (JAXP 1.0) and JSR 63 (JAXP 1.1 and 1.2). [[J2SE]] 1.4 is the first version of Java that comes with an implementation of JAXP. [[As of 2006]], the current version of JAXP is version 1.2 and JAXP 1.3 is being developed under JSR 206.
 
=== DOM interface ===
 
The DOM interface is perhaps the easiest to understand. It parses an entire XML document and constructs a complete in-memory representation of the document using the classes modeling the concepts found in the [http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113 Document Object Model(DOM) Level 2 Core Specification].
 
The DOM parser is called a <code>DocumentBuilder</code>, as it builds an in-memory <code>Document</code> representation. The {{Javadoc:SE|package=javax.xml.parsers|javax/xml/parsers|DocumentBuilder}} is created by the {{Javadoc:SE|package=javax.xml.parsers|javax/xml/parsers|DocumentBuilderFactory}}. The <code>DocumentBuilder</code> creates an {{Javadoc:SE|package=org.w3c.dom|org/w3c/dom|Document}} instance, which is a tree structure containing nodes in the XML Document. Each tree node in the structure implements the {{Javadoc:SE|package=org.w3c.dom|org/w3c/dom|Node}} interface. There are many different types of tree nodes, representing the type of data found in an XML document. The most important node types are:
* element nodes which may have attributes
* text nodes representing the text found between the start and end tags of a document element.
 
Refer to the [[Javadoc]] documentation of the [[Packages in Java|package]] {{Javadoc:SE|package=org.w3c.dom|org/w3c/dom}} for a complete list of node types.
 
=== SAX interface ===
 
The SAX parser is called the {{Javadoc:SE|javax/xml/parsers|SAXParser}} and is created by the {{Javadoc:SE|package=javax.xml.parsers|javax/xml/parsers|SAXParserFactory}}. Unlike the DOM parser, the SAX parser does not create an in-memory representation of the XML document and so is faster and uses less memory. Instead, the SAX parser informs clients of the XML document structure by invoking callbacks, that is, by invoking methods on a {{Javadoc:SE|package=org.xml.sax.helpers|org/xml/sax/helpers|DefaultHandler}} instance provided to the parser.
 
The <code>DefaultHandler</code> class implements the {{Javadoc:SE|org/xml/sax|ContentHandler}}, the {{Javadoc:SE|org/xml/sax|ErrorHandler}}, the {{Javadoc:SE|org/xml/sax|DTDHandler}}, and the {{Javadoc:SE|org/xml/sax|EntityResolver}} interfaces. Most clients will be interested in methods defined in the <code>ContentHandler</code> interface which are called when the SAX parser encounters the corresponding elements in the XML document. The most important methods in this interface are:
* <code>startDocument()</code> and <code>endDocument()</code> methods that are called at the start and end of an XML document.
* <code>startElement()</code> and <code>endElement()</code> methods that are called at the start and end of a document element.
* <code>characters()</code> method that is called with the text data contents contained between the start and end tags of an XML document element.
 
Clients provide a subclass of the <code>DefaultHandler</code> that overrides these methods and processes the data. This may involve storing the data into a database or writing it out to a stream.
 
=== XSLT interface ===
 
The '''X'''ML '''S'''tylesheet '''L'''anguage for '''T'''ransformations, or '''[[XSLT]]''', allows for conversion of an XML document into other forms of data.
 
=== External links ===
 
* See [http://java.sun.com/webservices/jaxp/ Sun's JAXP product description]
* [http://www.jcp.org/en/jsr/detail?id=63 JSR 63] (JAXP 1.1 and 1.2)
* [http://www.jcp.org/en/jsr/detail?id=5 JSR 5] (JAXP 1.0)
* See [http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113 Document Object Model(DOM) Level 2 Core Specification]
* Sample programs using DOM and SAX parser [http://totheriver.com/learn/xml/xmltutorial.html Tutorial: XML with Xerces for Java]
 
[[Category:Java platform]]
[[Category:Java specification requests]]
[[Category:XML-based standards]]
[[Category:Application programming interfaces]]
 
[[fr:Java API for XML Processing]]
[[vi:JAXP]]