jaxp_ppt
TRANSCRIPT
-
8/12/2019 JAXP_PPT
1/22
-
8/12/2019 JAXP_PPT
2/22
JAXP
-
8/12/2019 JAXP_PPT
3/22
Course Schedule
Day 1 of 1
Understand the need of JAXP
XML processors
JAXP Functionality
Parsing using DOM
JAXP Transformers
Demo exercises
-
8/12/2019 JAXP_PPT
4/22
Course Objectives
Understand the need of JAXP
Understand the XML processors
Understand the JAXP Functionality
Learn parsing using DOM and JAXP Transformers
-
8/12/2019 JAXP_PPT
5/22
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?
[Partly based on tutorial An Overview of the APIs available at
http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/overview/3_apis.html, from which also
some graphics are borrowed]
-
8/12/2019 JAXP_PPT
6/22
JAXP 1.1
An interface for plugging-in and using XML
processors in Java applications
includes packages
org.w3c.dom: DOM Level 2 interface
javax.xml.parsers:
initialization and use of parsers
javax.xml.transform:
initialization and use of transformers
(XSLT processors)
Included in JDK starting from vers. 1.4
-
8/12/2019 JAXP_PPT
7/22
JAXP: XML processor plugin (1)
Vendor-independent method for selecting processor
implementation at run time
principally through system properties
javax.xml.parsers.DocumentBuilderFactory, and
javax.xml.transform.TransformerFactory
For example:
System.setProperty(
"javax.xml.parsers.DocumentBuilderFactory",
"com.icl.saxon.om.DocumentBuilderFactoryImpl");
-
8/12/2019 JAXP_PPT
8/22
JAXP: XML processor plugin (2)
By default, reference implementations used
Apache Crimson/Xerces as the XML parser
Apache Xalan as the XSLT processor
Currently supported only by a few compliant XML processors:
Parsers: Apache Crimson and Xerces, Aelfred
XSLT transformers: Apache Xalan, Saxon
-
8/12/2019 JAXP_PPT
9/22
JAXP: Functionality
Parsing using DOM Level 2
Transformation using XSLT
(Well perform stand-alone transformations later)
Fixes features left unspecified in DOM Level 2
control of parser validation and error handling
creation and saving of DOM Document objects
-
8/12/2019 JAXP_PPT
10/22
JAXP Parsing API
Included in JAXP package
javax.xml.parsers
Used for invoking and using DOM parser implementations:
DocumentBuilderFactorydbf =
DocumentBuilderFactory.newInstance();
-
8/12/2019 JAXP_PPT
11/22
f.f.xmlxml
parseparse((
f.f.xmlxml)) newDocumentnewDocument()()
JAXP: Using a DOM parser (1)
-
8/12/2019 JAXP_PPT
12/22
JAXP: Using a DOM parser (2)
Weve used this, too:
DocumentBuilderFactoryDocumentBuilderFactorydbf =dbf =
DocumentBuilderFactoryDocumentBuilderFactory..newInstancenewInstance();();
try {try {// to get a new// to get a newDocumentBuilderDocumentBuilder::
documentBuilderdocumentBuilderbuilder =builder =
dbf.dbf.newDocumentBuildernewDocumentBuilder();();
} catch (} catch (ParserConfigurationExceptionParserConfigurationExceptione) {e) {
e.printStackTracee.printStackTrace());());
System.exit(1);System.exit(1);
};};
TestDOMParsing.javaTestDOMParsing.java
-
8/12/2019 JAXP_PPT
13/22
DOM building in JAXP
XMLXML
ReaderReader
(SAX(SAX
ParserParser))
XMLXML
ErrorErrorHandlerHandler
DTDDTDHandlerHandler
EntityEntityResolverResolver
DocumentDocumentBuilderBuilder
((ContentContent
HandlerHandler))
DOMDOM DocumentDocument
DOM on top of SAXDOM on top of SAX -- So whatSo what??
-
8/12/2019 JAXP_PPT
14/22
JAXP: Controlling parsing
Errors of DOM parsing can be handled
by creating a ErrorHandler, which implements error, fatalError and
warning methods, and passing it with setErrorHandler to theDocumentBuilder
setValidatingsetValidating(boolean(boolean)) andandsetNamespaceAwaresetNamespaceAware(boolean(boolean))
Validation and namespace processing can be controlled for
DocumentBuilderFactories with
-
8/12/2019 JAXP_PPT
15/22
JAXP Transformation API
also known as TrAX
Allows application to apply a Transformer to a Source document to get
a Result document
Transformer can be created
from XSLT transformation instructions (to be discussed later)
without instructions, which gives an identity transformation (simply copies
Source to Result)
-
8/12/2019 JAXP_PPT
16/22
XSLTXSLT
JAXP: Using Transformers (1)
-
8/12/2019 JAXP_PPT
17/22
JAXP Transformation Packages
javax.xml.transform:
Classes Transformer and TransformerFactory; initialization
similar to parsers and parser factories
Transformation Source object can be a DOM tree or an I/O stream
Transformation Result object can be
a DOM tree or an I/O stream
-
8/12/2019 JAXP_PPT
18/22
JAXP Transformation Packages (2)
Classes to create Source and Result objects from DOM and I/O
streams defined in packages
javax.xml.transform.dom, and
javax.xml.transform.stream
An identity transformation from a DOM Document to I/O stream
a vendor-neutral way to serialize DOM documents
(the only option in JAXP)
-
8/12/2019 JAXP_PPT
19/22
Serializing a DOM Document as XML text
Identity transformation to an I/O stream Result:
TransformerFactoryTransformerFactorytFactorytFactory==
TransformerFactoryTransformerFactory..newInstancenewInstance();();//// CreateCreate anan identity transformeridentity transformer::
TransformerTransformertransformertransformer==
tFactorytFactory..newTransformernewTransformer();();
DOMSourceDOMSourcesourcesource= new= newDOMSourceDOMSource((myDOMdocmyDOMdoc););
StreamResultStreamResultresultresult==
newnew StreamResultStreamResult((SystemSystem.out);.out);
transformertransformer..transformtransform((sourcesource,,resultresult););
Transform.javaTransform.java
-
8/12/2019 JAXP_PPT
20/22
Other Java APIs for XML
JDOM
variant of W3C DOM; closer to Java object-orientation
(http://www.jdom.org/)
DOM4J (http://www.dom4j.org/)
roughly similar to JDOM; richer set of features
JAXB (Java Architecture for XML Binding)
compiles DTDs to DTD-specific classes that allow to read, to
manipulate and to write valid documents
http://java.sun.com/xml/jaxb/
-
8/12/2019 JAXP_PPT
21/22
JAXP: Summary
An interface for using XML Processors
DOM parsers, XSLT transformers
Supports plugability of different implementations Defines means to control validation, and handling of parse errors
Defines means to write out DOM Documents
Included in JDK 1.4
-
8/12/2019 JAXP_PPT
22/22
Thank You