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