Vous êtes sur la page 1sur 4

3.3 JAXP: Java API for XML Processing JAXP 1.

n How can applications use XML processors? n An interface for “plugging-in” and using XML
– A Java-based answer: through JAXP processors in Java applications
– includes packages
– An overview of the JAXP interface
» org.xml.sax: SAX 2.0 interface
» What does it specify?
» org.w3c.dom: DOM Level 2 interface
» What can be done with it?
» javax.xml.parsers:
» How do the JAXP components fit together?
initialization and use of parsers
[Partly based on tutorial “An Overview of the APIs” available at » javax.xml.transform:
http://java .sun.com/xml/jaxp/dist/1.1/docs/tutorial/overview initialization and use of transformers
/3_apis.html, from which also some graphics are borrowed] (XSLT processors)
n Included in JDK starting from vers. 1.4
SDPL 2002 Notes 3: XML Processor Interfaces 1 SDPL 2002 Notes 3: XML Processor Interfaces 2

JAXP: XML processor plugin (1) JAXP: XML processor plugin (2)

n Vendor-independent method for selecting n By default, reference implementations used


processor implementation at run time – Apache Crimson/Xerces as the XML parser
– principally through system properties – Apache Xalan as the XSLT processor
javax.xml.parsers.SAXParserFactory ,
javax.xml.parsers.DocumentBuilderFactory , and n Currently supported only by a few compliant
javax.xml.transform.TransformerFactory XML processors:
– For example: – Parsers: Apache Crimson and Xerces, Aelfred
System.setProperty(
"javax.xml.parsers.DocumentBuilderFactory", – XSLT transformers: Apache Xalan, Saxon
"com.icl.saxon.om.DocumentBuilderFactoryImpl
");

SDPL 2002 Notes 3: XML Processor Interfaces 3 SDPL 2002 Notes 3: XML Processor Interfaces 4

JAXP: Functionality JAXP Parsing API

n Parsing using SAX 2.0 or DOM Level 2 n Included in JAXP package


n Transformation using XSLT javax.xml.parsers
– (We’ll perform stand-alone transformations later)
n Used for invoking and using SAX and
n Fixes features left unspecified in SAX 2.0 and
DOM parser implementations:
DOM Level 2
– control of parser validation and error handling SAXParserFactory spf =
– creation and saving of DOM Document objects SAXParserFactory.newInstance();

DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
SDPL 2002 Notes 3: XML Processor Interfaces 5 SDPL 2002 Notes 3: XML Processor Interfaces 6

1
JAXP: Using an SAX parser (1) JAXP: Using an SAX parser (2)

getXMLReader
n We’ve already used this:
SAXParserFactory spf =
SAXParserFactory.newInstance();
XML try {
SAXParser saxParser = spf.newSAXParser ();
XMLReader xmlReader =
saxParser.getXMLReader ();
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
};

SDPL 2002 Notes 3: XML Processor Interfaces 7 SDPL 2002 Notes 3: XML Processor Interfaces 8

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

n We’ve used this, too:


DocumentBuilderFactory dbf =
parse(
newDocument() DocumentBuilderFactory .newInstance ();
”f.xml”)
try { // to get a new DocumentBuilder:
documentBuilder builder =
dbf.newDocumentBuilder ();
} catch (ParserConfigurationException e) {
e.printStackTrace());
f.xml System.exit(1);
};

SDPL 2002 Notes 3: XML Processor Interfaces 9 SDPL 2002 Notes 3: XML Processor Interfaces 10

DOM building in JAXP JAXP: Controlling parsing

Document n Errors of DOM parsing can be handled


Builder
– by creating a SAX ErrorHandler, which implements
(Content
Handler) error, fatalError and warning methods, and
passing it with setErrorHandler to the
XML Error DOM Document DocumentBuilder
Reader Handler
XML (SAX DTD n Validation and namespace processing can be
Parser) Handler controlled, both for SAXParserFactories and
Entity DocumentBuilderFactories with
Resolver
setValidating(boolean) and
DOM on top of SAX - So what? setNamespaceAware(boolean)
SDPL 2002 Notes 3: XML Processor Interfaces 11 SDPL 2002 Notes 3: XML Processor Interfaces 12

2
JAXP Transformation API JAXP: Using Transformers (1)

n also known as TrAX


n Allows application to apply a Transformer to a
Source document to get a Result document
n Transformer can be created
– from XSLT transformation instructions (to be
discussed later)
– without instructions, which gives an identity
transformation (simply copies Source to Result) XSLT

SDPL 2002 Notes 3: XML Processor Interfaces 13 SDPL 2002 Notes 3: XML Processor Interfaces 14

JAXP Transformation Packages JAXP Transformation Packages (2)

n javax.xml.transform: n Classes to create Source and Result objects


– Classes Transformer and from DOM, SAX and I/O streams defined in
TransformerFactory; initialization similar packages
to parsers and parser factories – javax.xml.transform.dom,
javax.xml.transform.sax, and
n Transformation Source object can be javax.xml.transform.stream
– a DOM tree, an SAX XMLReader or an I/O stream
n An identity transformation from a DOM
n Transformation Result object can be
Document to I/O stream a vendor-neutral
– a DOM tree, an SAX ContentHandler or an I/O way to serialize DOM documents
stream
– (the only option in JAXP)
SDPL 2002 Notes 3: XML Processor Interfaces 15 SDPL 2002 Notes 3: XML Processor Interfaces 16

Serializing a DOM Document as XML text Other Java APIs for XML

n Identity transformation to an I/O stream Result: n JDOM


– variant of W3C DOM; closer to Java object-
TransformerFactory tFactory = orientation (http://www.jdom.org/)
TransformerFactory.newInstance();
n DOM4J (http://www.dom4j.org/)
// Create an identity transformer:
– roughly similar to JDOM; richer set of features
Transformer transformer =
tFactory.newTransformer(); n JAXB (Java Architecture for XML Binding)
DOMSource source = new DOMSource(myDOMdoc); – compiles DTDs to DTD-specific classes that allow to
StreamResult result =
read, to manipulate and to write valid documents
new StreamResult(System.out);
transformer.transform(source, result); – http://java.sun.com/xml/jaxb/

SDPL 2002 Notes 3: XML Processor Interfaces 17 SDPL 2002 Notes 3: XML Processor Interfaces 18

3
JAXP: Summary

n An interface for using XML Processors


– SAX/DOM parsers, XSLT transformers
n Supports plugability of different
implementations
n Defines means to control validation, and
handling of parse errors (through SAX
ErrorHandlers)
n Defines means to write out DOM Documents
n Included in JDK 1.4
SDPL 2002 Notes 3: XML Processor Interfaces 19

Vous aimerez peut-être aussi