25
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]

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

Embed Size (px)

Citation preview

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

controlling schema-based controlling schema-based validation:validation:– http://java.sun.com/xml/jaxp/http://java.sun.com/xml/jaxp/properties/schemaLanguageproperties/schemaLanguage

– http://java.sun.com/xml/jaxp/http://java.sun.com/xml/jaxp/properties/schemaSourceproperties/schemaSource

SDPL 2004 Notes 3: XML Processor Interfaces 4

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

javax.xml.parsers.SAXParserFactoryjavax.xml.parsers.SAXParserFactoryjavax.xml.parsers.DocumentBuilderFactoryjavax.xml.parsers.DocumentBuilderFactoryjavax.xml.transform.TransformerFactoryjavax.xml.transform.TransformerFactory

– 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 …

SAXParserFactorySAXParserFactory spf = spf =SAXParserFactorySAXParserFactory..newInstancenewInstance();();

and DOM parser implementations:and DOM parser implementations:

DocumentBuilderFactoryDocumentBuilderFactory dbf = dbf =DocumentBuilderFactoryDocumentBuilderFactory..newInstancenewInstance();();

SDPL 2004 Notes 3: XML Processor Interfaces 8

XMLXML

.getXMLReader().getXMLReader()

JAXP: Using a SAX parser JAXP: Using a SAX parser (1)(1)

f.xmlf.xml

.parse(.parse( ” ”f.xml”)f.xml”)

.newSAXParser().newSAXParser()

SDPL 2004 Notes 3: XML Processor Interfaces 9

JAXP: Using a SAX parser JAXP: Using a SAX parser (2)(2)

We have already seen this:We have already seen this:SAXParserFactorySAXParserFactory spf = spf =

SAXParserFactorySAXParserFactory..newInstancenewInstance();(); trytry { {

SAXParserSAXParser saxParser = spf. saxParser = spf.newSAXParsernewSAXParser();(); XMLReaderXMLReader xmlReader = xmlReader =

saxParser.saxParser.getXMLReadergetXMLReader();(); ... ... xmlReaderxmlReader..setContentHandlersetContentHandler(handler);(handler); xmlReaderxmlReader..parseparse(fileName); ... (fileName); ...

} } catchcatch (Exception e) { (Exception e) {System.err.println(e.getMessage());System.err.println(e.getMessage());System.exit(1); };System.exit(1); };

SDPL 2004 Notes 3: XML Processor Interfaces 10

f.xmlf.xml

JAXP: Using a DOM parser JAXP: Using a DOM parser (1)(1)

.parse(”f.xml”).parse(”f.xml”)

.newDocument().newDocument()

.newDocumentBuilder().newDocumentBuilder()

SDPL 2004 Notes 3: XML Processor Interfaces 11

JAXP: Using a DOM parser JAXP: Using a DOM parser (2)(2)

Parsing a file into a DOM Parsing a file into a DOM Document:Document:DocumentBuilderFactoryDocumentBuilderFactory dbf = dbf =

DocumentBuilderFactoryDocumentBuilderFactory..newInstancenewInstance();(); trytry { { // to get a new// to get a new DocumentBuilderDocumentBuilder::

DocumentBuilderDocumentBuilder builder = builder = dbf.dbf.newDocumentBuildernewDocumentBuilder(); ();

DocumentDocument domDoc = domDoc = builder.builder.parseparse(fileName);(fileName);

} } catchcatch ( (ParserConfigurationExceptionParserConfigurationException e) { e) {e.printStackTrace());e.printStackTrace());System.exit(1); System.exit(1);

}; };

SDPL 2004 Notes 3: XML Processor Interfaces 12

DOM building in JAXPDOM building in JAXP

XMLXMLReaderReader

(SAX(SAXParser)Parser)

XMLXML

ErrorErrorHandlerHandler

DTDDTDHandlerHandler

EntityEntityResolverResolver

DocumentDocumentBuilderBuilder

(Content(ContentHandler)Handler)

DOM DocumentDOM Document

DOM on top of SAX - So what?DOM on top of SAX - So what?

SDPL 2004 Notes 3: XML Processor Interfaces 13

JAXP: Controlling parsing JAXP: Controlling parsing (1)(1)

Errors of DOM parsing can be handled Errors of DOM parsing can be handled – by creating a SAXby creating a SAX ErrorHandler ErrorHandler

» to implement to implement errorerror, , fatalErrorfatalError and and warningwarning methods methods

and passing it to the and passing it to the DocumentBuilderDocumentBuilder::builder.builder.setErrorHandlersetErrorHandler(new (new

myErrHandler()); myErrHandler()); domDoc = builder.domDoc = builder.parseparse(fileName);(fileName);

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)

SDPL 2004 Notes 3: XML Processor Interfaces 14

JAXP: Controlling parsing JAXP: Controlling parsing (2)(2)

setIgnoringCommentssetIgnoringComments(true/false)(true/false)

setIgnoringElementContentWhitespacesetIgnoringElementContentWhitespace(true/false)(true/false)

setCoalescingsetCoalescing(true/false)(true/false)

• combine CDATA sections with surrounding text?combine CDATA sections with surrounding text?

setExpandEntityReferencessetExpandEntityReferences(true/false)(true/false)

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

SDPL 2004 Notes 3: XML Processor Interfaces 18

Source-Result Source-Result combinationscombinations

XMLXMLReaderReader

(SAX (SAX Parser)Parser)

TransformTransformerer

DOMDOM

ConteContentnt

HandlHandlerer

Input Input StreamStream

Output Output StreamStream

DOMDOM

SourceSource ResultResult

SDPL 2004 Notes 3: XML Processor Interfaces 19

JAXP Transformation Packages JAXP Transformation Packages (2)(2)

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:

TransformerFactoryTransformerFactory tFactory = tFactory = TransformerFactoryTransformerFactory..newInstancenewInstance();();

// Create an identity transformer:// Create an identity transformer:TransformerTransformer transformer = transformer =

tFactory.tFactory.newTransformernewTransformer(); (); DOMSourceDOMSource source = source = newnew DOMSourceDOMSource(myDOMdoc); (myDOMdoc); StreamResultStreamResult result = result =

newnew StreamResultStreamResult(System.out); (System.out);

transformer.transformer.transformtransform(source, result);(source, result);

SDPL 2004 Notes 3: XML Processor Interfaces 21

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::

<xsl:transform version="1.0" <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output encoding="ISO-8859-1" indent="yes"<xsl:output encoding="ISO-8859-1" indent="yes" doctype-system="reglist.dtd" /> doctype-system="reglist.dtd" />

<xsl:template match="/"> <!--copy entire doc:--><xsl:template match="/"> <!--copy entire doc:--> <xsl:copy-of select="." /><xsl:copy-of select="." /> </xsl:template> </xsl:template>

</xsl:transform></xsl:transform>

SDPL 2004 Notes 3: XML Processor Interfaces 22

Creating an XSLT TransformerCreating an XSLT Transformer

Then create a tailored transfomer:Then create a tailored transfomer:

StreamSourceStreamSource saveSpecSrc = saveSpecSrc = new new StreamSourceStreamSource((

new new File(”saveSpec.xslt”) );File(”saveSpec.xslt”) );TransformerTransformer transformer = transformer =

tFactory.tFactory.newTransformernewTransformer(saveSpecSrc); (saveSpecSrc);

// 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

Included in Java 2Included in Java 2