XPT 2006 XML APIs: JAXP 1
2.3 JAXP: Java API for XML Processing 2.3 JAXP: Java API for XML Processing
- How can applications use XML processors?
How can applications use XML processors?
– – A Java A Java-
- based answer: through
based answer: through JAXP JAXP – – An overview of the JAXP interface An overview of the JAXP interface
» » What does it specify? What does it specify? » » What can be done with it? What can be done with it? » » How do the JAXP components fit together? How do the JAXP components fit together?
[Partly based on tutorial [Partly based on tutorial “ “An Overview of the APIs An Overview of the APIs” ” at at http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/overview http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/overview /3_apis.html, from which also some graphics are borrowed] /3_apis.html, from which also some graphics are borrowed]
XPT 2006 XML APIs: JAXP 2
JAXP 1.1 JAXP 1.1
- Included in Java since JDK 1.4
Included in Java since JDK 1.4
- An interface for
An interface for “ “plugging plugging-
- in
in” ” and using XML and using XML processors in Java applications processors in Java applications
– – includes packages includes packages
» » org.xml.sax
- rg.xml.sax:
: SAX 2.0 SAX 2.0 » » org.w3c.dom:
- rg.w3c.dom: DOM Level 2
DOM Level 2 » » javax.xml.parsers javax.xml.parsers: : initialization and use of parsers initialization and use of parsers » » javax.xml.transform javax.xml.transform: : initialization and use of transformers initialization and use of transformers (XSLT processors) (XSLT processors)
XPT 2006 XML APIs: JAXP 3
Later Versions Later Versions
- JAXP 1.2 (2002) adds property
JAXP 1.2 (2002) adds property-
- strings for
strings for setting the language and source of a schema setting the language and source of a schema used for (non used for (non-
- DTD
DTD-
- based) validation
based) validation
- JAXP 1.3 included in JDK 5.0 (2005)
JAXP 1.3 included in JDK 5.0 (2005)
– – more flexible validation (decoupled from parsing) more flexible validation (decoupled from parsing) – – support for DOM3 and support for DOM3 and XPath XPath
- We'll restrict to basic ideas from JAXP 1.1
We'll restrict to basic ideas from JAXP 1.1
XPT 2006 XML APIs: JAXP 4
JAXP: XML processor JAXP: XML processor plugin plugin (1) (1)
- Vendor
Vendor-
- independent method for selecting
independent method for selecting processor implementation at run time processor implementation at run time
– – principally through system properties principally through system properties
javax.xml.parsers.SAXParserFactory javax.xml.parsers.SAXParserFactory javax.xml.parsers.DocumentBuilderFactory javax.xml.parsers.DocumentBuilderFactory javax.xml.transform.TransformerFactory javax.xml.transform.TransformerFactory
– – Set on command line (for example, to use Apache Set on command line (for example, to use Apache Xerces Xerces as the DOM implementation): as the DOM implementation):
java java
- D
Djavax.xml.parsers.DocumentBuilderFactory
javax.xml.parsers.DocumentBuilderFactory= =
- rg.apache.xerces.jaxp.DocumentBuilderFactoryImpl
- rg.apache.xerces.jaxp.DocumentBuilderFactoryImpl
XPT 2006 XML APIs: JAXP 5
JAXP: XML processor JAXP: XML processor plugin plugin (2) (2)
– – Set during execution ( Set during execution (− −> > Saxon as the XSLT Saxon as the XSLT impl impl): ):
System.setProperty System.setProperty( ( " "javax.xml.transform.TransformerFactory javax.xml.transform.TransformerFactory", ", " "com.icl.saxon.TransformerFactoryImpl com.icl.saxon.TransformerFactoryImpl"); ");
- By default, reference implementations used
By default, reference implementations used
– – Apache Crimson/ Apache Crimson/Xerces Xerces as the XML parser as the XML parser – – Apache Apache Xalan Xalan as the XSLT processor as the XSLT processor
- Supported by a few compliant processors:
Supported by a few compliant processors:
– – Parsers: Apache Crimson and Parsers: Apache Crimson and Xerces Xerces, , Aelfred Aelfred, , Oracle XML Parser for Java Oracle XML Parser for Java – – XSLT transformers: Apache XSLT transformers: Apache Xalan Xalan, Saxon , Saxon
XPT 2006 XML APIs: JAXP 6
JAXP: Functionality JAXP: Functionality
- Parsing using SAX 2.0 or DOM Level 2
Parsing using SAX 2.0 or DOM Level 2
- Transformation using XSLT
Transformation using XSLT
– – (We (We’ ’ll study XSLT in detail later) ll study XSLT in detail later)
- Adds functionality missing from SAX 2.0 and
Adds functionality missing from SAX 2.0 and DOM Level 2: DOM Level 2:
– – controlling validation and handling of parse errors controlling validation and handling of parse errors
» » error handling error handling can can be controlled in SAX, be controlled in SAX, by implementing by implementing ErrorHandler ErrorHandler methods methods
– – loading and saving of DOM Document objects loading and saving of DOM Document objects
XPT 2006 XML APIs: JAXP 7
JAXP JAXP Parsing Parsing API API
- Included
Included in JAXP in JAXP package package
javax.xml.parsers javax.xml.parsers
- Used for invoking and using SAX
Used for invoking and using SAX … …
SAXParserFactory SAXParserFactory spf spf = = SAXParserFactory SAXParserFactory. .newInstance newInstance(); ();
and DOM parser implementations: and DOM parser implementations:
DocumentBuilderFactory DocumentBuilderFactory dbf = dbf = DocumentBuilderFactory DocumentBuilderFactory. .newInstance newInstance(); ();
XPT 2006 XML APIs: JAXP 8
XML XML
. .getXMLReader getXMLReader() ()
JAXP: JAXP: Using Using a SAX a SAX parser parser (1) (1)
f.xml f.xml . .parse parse( ( ” ”f.xml f.xml” ”) ) . .newSAXParser newSAXParser() ()