SDPL 2003 Notes 3: XML Processor In terfaces 1 3.3 JAXP: Java API for XML 3.3 JAXP: Java API for XML Processing Processing How can applications use XML processors? How can applications use XML processors? – A Java-based answer: through A Java-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 “An Overview of the APIs” [Partly based on tutorial “An Overview of the APIs” available at available at http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/ov http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/ov erview/3_apis.html, from which also some graphics are erview/3_apis.html, from which also some graphics are borrowed] borrowed]
3.3 JAXP: Java API for XML Processing. How can applications use XML processors? A Java-based answer: through JAXP An overview of the JAXP interface What does it specify? What can be done with it? How do the JAXP components fit together? - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SDPL 2003 Notes 3: XML Processor Interfaces 1
3.3 JAXP: Java API for XML 3.3 JAXP: Java API for XML ProcessingProcessing
How can applications use XML processors?How can applications use XML processors?– A Java-based answer: through A Java-based answer: through JAXPJAXP– An overview of the JAXP interfaceAn 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 “An Overview of the APIs” [Partly based on tutorial “An Overview of the APIs” available at available at http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/overvihttp://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/overview/3_apis.html, from which also some graphics are ew/3_apis.html, from which also some graphics are borrowed]borrowed]
SDPL 2003 Notes 3: XML Processor Interfaces 2
JAXP 1.1JAXP 1.1
An interface for “plugging-in” and using An interface for “plugging-in” and using XML processors in Java applicationsXML processors in Java applications– includes packagesincludes packages
» org.xml.sax:org.xml.sax: SAX 2.0 interface SAX 2.0 interface» org.w3c.dom:org.w3c.dom: DOM Level 2 interface DOM Level 2 interface» javax.xml.parsersjavax.xml.parsers::
initialization and use of parsersinitialization and use of parsers» javax.xml.transformjavax.xml.transform::
initialization and use of initialization and use of transformers transformers
(XSLT processors)(XSLT processors)
Included in JDK starting from vers. 1.4Included in JDK starting from vers. 1.4
SDPL 2003 Notes 3: XML Processor Interfaces 3
JAXP: XML processor plugin JAXP: XML processor plugin (1)(1)
Vendor-independent method for selecting Vendor-independent method for selecting processor implementation at run timeprocessor implementation at run time– principally through system propertiesprincipally through system properties
– Set on command line (to use Apache Xerces Set on command line (to use Apache Xerces as the DOM implementation):as the DOM implementation): java java -D-Djavax.xml.parsers.DocumentBuilderFactoryjavax.xml.parsers.DocumentBuilderFactory= = org.apache.xerces.jaxp.DocumentBuilderFactoryImplorg.apache.xerces.jaxp.DocumentBuilderFactoryImpl
SDPL 2003 Notes 3: XML Processor Interfaces 4
JAXP: XML processor plugin JAXP: XML processor plugin (2)(2)
– Set during execution (Set during execution ( Saxon as the XSLT impl): Saxon as the XSLT impl): System.setProperty(System.setProperty(""javax.xml.transform.TransformerFactoryjavax.xml.transform.TransformerFactory", ", "com.icl.saxon.TransformerFactoryImpl");"com.icl.saxon.TransformerFactoryImpl");
By default, reference implementations usedBy default, reference implementations used– Apache Crimson/Xerces as the XML parserApache Crimson/Xerces as the XML parser– Apache Xalan as the XSLT processorApache Xalan as the XSLT processor
Currently supported only by a few compliant Currently supported only by a few compliant XML processors:XML processors:– Parsers: Apache Crimson and Xerces, Aelfred, Parsers: Apache Crimson and Xerces, Aelfred,
Oracle XML Parser for Java Oracle XML Parser for Java– XSLT transformers: Apache Xalan, SaxonXSLT transformers: Apache Xalan, Saxon
SDPL 2003 Notes 3: XML Processor Interfaces 5
JAXP: FunctionalityJAXP: Functionality
Parsing using SAX 2.0 or DOM Level 2Parsing using SAX 2.0 or DOM Level 2 Transformation using XSLTTransformation using XSLT
– (We’ll study XSLT in detail later)(We’ll study XSLT in detail later) Fixes features left unspecified in SAX 2.0 Fixes features left unspecified in SAX 2.0
and DOM 2 interfacesand DOM 2 interfaces– control of parser validation and error control of parser validation and error
handlinghandling» error handling error handling cancan be controlled in SAX by be controlled in SAX by
Errors of DOM parsing can be handled Errors of DOM parsing can be handled – by creating a SAXby creating a SAX ErrorHandler ErrorHandler, say , say errHandlererrHandler
» to implement to implement errorerror, , fatalErrorfatalError and and warningwarning methods methods
and passing it to the and passing it to the DocumentBuilder DocumentBuilder (before (before parsing)parsing)::
builder.builder.setErrorHandlersetErrorHandler(errHandler);(errHandler); Parser properties can be configured:Parser properties can be configured:
– for both for both SAXParserFactoriesSAXParserFactories and and DocumentBuilderFactories DocumentBuilderFactories (before parser (before parser creation):creation):factory.factory.setValidatingsetValidating(true/false)(true/false)factory.factory.setNamespaceAwaresetNamespaceAware(true/false)(true/false)
Further Further DocumentBuilderFactoryDocumentBuilderFactory configuration methods to control the configuration methods to control the form of the resulting DOM tree:form of the resulting DOM tree:
SDPL 2003 Notes 3: XML Processor Interfaces 14
JAXP Transformation APIJAXP Transformation API
earlier known as TrAXearlier known as TrAX Allows application to apply a Allows application to apply a TransformerTransformer
to a to a SourceSource document to get a document to get a ResultResult documentdocument
TransformerTransformer can be created can be created – from XSLT transformation instructions (to be from XSLT transformation instructions (to be
discussed later)discussed later)– without instructionswithout instructions
» gives an identity transformation, which simply gives an identity transformation, which simply copies the copies the SourceSource to the to the ResultResult
SDPL 2003 Notes 3: XML Processor Interfaces 15
XSLTXSLT
JAXP: Using Transformers JAXP: Using Transformers (1)(1)
javax.xml.transformjavax.xml.transform:: – Classes Classes TransformerTransformer and and TransformerFactoryTransformerFactory; initialization similar ; initialization similar to parsers and parser factoriesto parsers and parser factories
Transformation Transformation SourceSource object can be object can be– a DOM tree, a SAX XMLReader or an input a DOM tree, a SAX XMLReader or an input
streamstream Transformation Transformation ResultResult object can be object can be
– a DOM tree, a SAX ContentHandler a DOM tree, a SAX ContentHandler or an output streamor an output stream
Classes to create Classes to create SourceSource and and ResultResult objects from DOM, SAX and I/O streams objects from DOM, SAX and I/O streams defined in packagesdefined in packages– javax.xml.transform.domjavax.xml.transform.dom,, javax.xml.transform.saxjavax.xml.transform.sax,, andand javax.xml.transform.streamjavax.xml.transform.stream
An identity transformation from a DOM An identity transformation from a DOM Document to I/O stream gives a vendor-Document to I/O stream gives a vendor-neutral way to serialize DOM documentsneutral way to serialize DOM documents– (the only option in JAXP)(the only option in JAXP)
SDPL 2003 Notes 3: XML Processor Interfaces 19
Serializing a DOM Document as XML Serializing a DOM Document as XML texttext
Identity transformation to an output stream:Identity transformation to an output stream:
Controlling the form of the result?Controlling the form of the result?
As above, but create a As above, but create a TransformerTransformer with XSLT with XSLT instructions as a instructions as a StreamSourceStreamSource, say , say saveSpecSrcsaveSpecSrc::
JDOMJDOM– a Java-specific variant of W3C DOM a Java-specific variant of W3C DOM – http://www.jdom.org/http://www.jdom.org/
DOM4J DOM4J ((http://www.dom4j.org/http://www.dom4j.org/)) – roughly similar to JDOM; richer set of features: roughly similar to JDOM; richer set of features: – powerful navigation with integrated XPath supportpowerful navigation with integrated XPath support
JAXB (Java Architecture for XML Binding)JAXB (Java Architecture for XML Binding)– compiles DTDs to DTD-specific classes for reading, compiles DTDs to DTD-specific classes for reading,
manipulating and writing valid documentsmanipulating and writing valid documents– http://java.sun.com/xml/jaxb/http://java.sun.com/xml/jaxb/
SDPL 2003 Notes 3: XML Processor Interfaces 22
JAXP: SummaryJAXP: Summary
An interface for using XML ProcessorsAn interface for using XML Processors– SAX/DOM parsers, XSLT transformersSAX/DOM parsers, XSLT transformers
Supports plugability of XML processorsSupports plugability of XML processors Defines means to control parsing and Defines means to control parsing and
handling of parse errors (through SAX handling of parse errors (through SAX ErrorHandlers)ErrorHandlers)
Defines means to write out DOM Defines means to write out DOM DocumentsDocuments