Java API for XML Processing: Difference between revisions

Content deleted Content added
No edit summary
SAE1962 (talk | contribs)
m Improved code formatting
Line 13:
 
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 whichthat may have attributes
* text nodes representing the text found between the start and end tags of a document element.
 
Line 22:
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 whichthat 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.
Line 33:
== 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. JAXP provides interfaces in package <code>javax.xml.transform</code> allowing applications to invoke an XSLT transformation. This interface was originally called TrAX (Transformation API for XML), and was developed by an informal collaboration between the developers of a number of Java XSLT processors.
 
Main features of the interface are:
Line 43:
* a method on the Templates object to create a Transformer, representing the executable form of a stylesheet. This cannot be shared across threads, though it is serially reusable. The Transformer provides methods to set stylesheet parameters and serialization options (for example, whether output should be indented), and a method to actually run the transformation.
 
Two abstract interfaces Source and Result are defined to represent the input and output of the transformation. This is a somewhat unconventional use of Java interfaces, since there is no expectation that a processor will accept any class that implements the interface - each processor can choose which kinds of Source or Result it is prepared to handle. In practice all JAXP processors support the three standard kinds of Source (<code>DOMSource</code>, <code>SAXSource</code>, <code>StreamSource</code>) and the three standard kinds of Result (<code>DOMResult</code>, <code>SAXResult</code>, <code>StreamResult</code>) and possibly other implementations of their own.
 
== 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 the DOM and SAX parser [http://totheriver.com/learn/xml/xmltutorial.html Tutorial: XML with Xerces for Java]
* [http://www.ibm.com/developerworks/xml/library/x-xjavaforum4.html Sun's Java and XML APIs: Helping or hurting?]
 
<!-- Categories -->
 
[[Category:Java platform|XML Processing]]
[[Category:Java specification requests|XML Processing]]
Line 60:
[[Category:Java APIs]]
 
<!-- Interwikis -->
[[de:Java API for XML Processing]]
[[es:JAXP]]