SDPL 2004 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 How can applications use XML processors? 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” at [Partly based on tutorial “An Overview of the APIs” at http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/ http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/ overview/3_apis.html, from which also some graphics overview/3_apis.html, from which also some graphics are borrowed] are borrowed]
25
Embed
SDPL 2004Notes 3: XML Processor Interfaces1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –A Java-based answer: through.
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 2004 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” at [Partly based on tutorial “An Overview of the APIs” at http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/overvhttp://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/overview/3_apis.html, from which also some graphics are iew/3_apis.html, from which also some graphics are borrowed]borrowed]
SDPL 2004 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 since version 1.4Included in JDK since version 1.4
SDPL 2004 Notes 3: XML Processor Interfaces 3
JAXP 1.2JAXP 1.2
Current version, since June 2002Current version, since June 2002 Adds property strings for Adds property strings for
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 (for example, to use Set on command line (for example, to use Apache Xerces as the DOM implementation):Apache Xerces as the DOM implementation): java java -D-Djavax.xml.parsers.DocumentBuilderFactoryjavax.xml.parsers.DocumentBuilderFactory= = org.apache.xerces.jaxp.DocumentBuilderFactoryImplorg.apache.xerces.jaxp.DocumentBuilderFactoryImpl
SDPL 2004 Notes 3: XML Processor Interfaces 5
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 2004 Notes 3: XML Processor Interfaces 6
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) Adds functionality missing from SAX 2.0 Adds functionality missing from SAX 2.0
and DOM Level 2:and DOM Level 2:– controlling validation and handling of parse controlling validation and handling of parse
errorserrors» error handling error handling cancan be controlled in SAX, be controlled in SAX,
by implementing by implementing ErrorHandlerErrorHandler methods methods
– loading and saving of DOM Document objectsloading and saving of DOM Document objects
SDPL 2004 Notes 3: XML Processor Interfaces 7
JAXP Parsing APIJAXP Parsing API
Included in JAXP package Included in JAXP package javax.xml.parsersjavax.xml.parsers
Used for invoking and using SAX …Used for invoking and using SAX …
Further Further DocumentBuilderFactoryDocumentBuilderFactory configuration methods to control the configuration methods to control the form of the resulting DOM Document:form of the resulting DOM Document:
SDPL 2004 Notes 3: XML Processor Interfaces 15
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 2004 Notes 3: XML Processor Interfaces 16
XSLTXSLT
JAXP: Using Transformers JAXP: Using Transformers (1)(1)
.newTransformer(…).newTransformer(…)
.transform(.,.).transform(.,.)
SourceSource
SDPL 2004 Notes 3: XML Processor Interfaces 17
JAXP Transformation APIsJAXP Transformation APIs
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
Identity transformation to an output stream is a Identity transformation to an output stream is a vendor-neutral way to serialize DOM documents vendor-neutral way to serialize DOM documents (and the only option in JAXP)(and the only option in JAXP)– ““I would recommend using the JAXP interfaces until the I would recommend using the JAXP interfaces until the
DOM’s own load/save module becomes available” DOM’s own load/save module becomes available” » Joe Kesselman, IBM & W3C DOM WGJoe Kesselman, IBM & W3C DOM WG
SDPL 2004 Notes 3: XML Processor Interfaces 20
Serializing a DOM Document as XML Serializing a DOM Document as XML texttext
By an identity transformation to an output stream:By an identity transformation to an output stream:
Controlling the form of the result?Controlling the form of the result?
We could specify the requested form of the result by We could specify the requested form of the result by an XSLT script, say, in file an XSLT script, say, in file saveSpecSrc.xsltsaveSpecSrc.xslt::
// and use it to transform a Source to a Result, // and use it to transform a Source to a Result,
// as before// as before
NB: Transformation instructions could be given also, NB: Transformation instructions could be given also, say, as a say, as a DOMSourceDOMSource
SDPL 2004 Notes 3: XML Processor Interfaces 23
Other Java APIs for XMLOther Java APIs for XML
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 2004 Notes 3: XML Processor Interfaces 24
Why then stick to DOM?Why then stick to DOM?
Other document models might be more Other document models might be more convenient to use, but …convenient to use, but … “ “The The DOM offersDOM offers not only the not only the ability to ability to
move between languagesmove between languages with minimal with minimal relearning, but to relearning, but to move between multiple move between multiple implementationsimplementations in a single language – in a single language – which a specific set of classes such as JDOM which a specific set of classes such as JDOM can’t support”can’t support”» J. Kesselman, IBM & W3C DOM WG J. Kesselman, IBM & W3C DOM WG
SDPL 2004 Notes 3: XML Processor Interfaces 25
JAXP: SummaryJAXP: Summary
An interface for using XML ProcessorsAn interface for using XML Processors– SAX/DOM parsers, XSLT transformersSAX/DOM parsers, XSLT transformers
Supports pluggability of XML processorsSupports pluggability 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