Presentation is loading. Please wait.

Presentation is loading. Please wait.

SDPL 2003Notes 3: XML Processor Interfaces1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –A Java-based answer: through.

Similar presentations


Presentation on theme: "SDPL 2003Notes 3: XML Processor Interfaces1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –A Java-based answer: through."— Presentation transcript:

1 SDPL 2003Notes 3: XML Processor Interfaces1 3.3 JAXP: Java API for XML Processing n 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]

2 SDPL 2003Notes 3: XML Processor Interfaces2 JAXP 1.1 n An interface for “plugging-in” and using XML processors in Java applications –includes packages »org.xml.sax: SAX 2.0 interface »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) n Included in JDK starting from vers. 1.4

3 SDPL 2003Notes 3: XML Processor Interfaces3 JAXP: XML processor plugin (1) n Vendor-independent method for selecting processor implementation at run time –principally through system properties javax.xml.parsers.SAXParserFactory javax.xml.parsers.DocumentBuilderFactory javax.xml.transform.TransformerFactory –Set on command line (to use Apache Xerces as the DOM implementation): java -D javax.xml.parsers.DocumentBuilderFactory= org.apache.xerces.jaxp.DocumentBuilderFactoryImpl

4 SDPL 2003Notes 3: XML Processor Interfaces4 JAXP: XML processor plugin (2) –Set during execution (  Saxon as the XSLT impl): System.setProperty( "javax.xml.transform.TransformerFactory", "com.icl.saxon.TransformerFactoryImpl"); n By default, reference implementations used –Apache Crimson/Xerces as the XML parser –Apache Xalan as the XSLT processor n Currently supported only by a few compliant XML processors: –Parsers: Apache Crimson and Xerces, Aelfred, Oracle XML Parser for Java –XSLT transformers: Apache Xalan, Saxon

5 SDPL 2003Notes 3: XML Processor Interfaces5 JAXP: Functionality n Parsing using SAX 2.0 or DOM Level 2 n Transformation using XSLT –(We’ll study XSLT in detail later) n Fixes features left unspecified in SAX 2.0 and DOM 2 interfaces –control of parser validation and error handling »error handling can be controlled in SAX by implementing ErrorHandler methods –loading and saving of DOM Document objects

6 SDPL 2003Notes 3: XML Processor Interfaces6 JAXP Parsing API Included in JAXP package javax.xml.parsers Included in JAXP package javax.xml.parsers Used for invoking and using SAX SAXParserFactory spf = SAXParserFactory.newInstance(); Used for invoking and using SAX SAXParserFactory spf = SAXParserFactory.newInstance(); and DOM parser implementations: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

7 SDPL 2003Notes 3: XML Processor Interfaces7 XML.getXMLReader() JAXP: Using a SAX parser (1) f.xml.parse( ”f.xml”) ”f.xml”).newSAXParser()

8 SDPL 2003Notes 3: XML Processor Interfaces8 JAXP: Using a SAX parser (2) n We have already seen this: SAXParserFactory spf = SAXParserFactory.newInstance(); try { try { SAXParser saxParser = spf.newSAXParser(); SAXParser saxParser = spf.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); XMLReader xmlReader = saxParser.getXMLReader(); } catch (Exception e) { } catch (Exception e) {System.err.println(e.getMessage()); System.exit(1); }; … xmlReader.setContentHandler(handler); xmlReader.parse(fileName);

9 SDPL 2003Notes 3: XML Processor Interfaces9 f.xml JAXP: Using a DOM parser (1).parse(”f.xml”).newDocument().newDocumentBuilder()

10 SDPL 2003Notes 3: XML Processor Interfaces10 JAXP: Using a DOM parser (2) n Code to parse a file into DOM: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory.newInstance(); try { // to get a new DocumentBuilder: documentBuilder builder = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace()); System.exit(1); }; Document domDoc = builder.parse(fileName);

11 SDPL 2003Notes 3: XML Processor Interfaces11 DOM building in JAXP XMLReader (SAXParser) XML ErrorHandler DTDHandler EntityResolver DocumentBuilder(ContentHandler) DOM Document DOM on top of SAX - So what?

12 SDPL 2003Notes 3: XML Processor Interfaces12 JAXP: Controlling parsing (1) n Errors of DOM parsing can be handled –by creating a SAX ErrorHandler, say errHandler » to implement error, fatalError and warning methods and passing it to the DocumentBuilder (before parsing) : builder.setErrorHandler(errHandler); n Parser properties can be configured: –for both SAXParserFactories and DocumentBuilderFactories (before parser creation): factory.setValidating(true/false) factory.setNamespaceAware(true/false)

13 SDPL 2003Notes 3: XML Processor Interfaces13 JAXP: Controlling parsing (2) setIgnoringComments(true/false) setIgnoringElementContentWhitespace(true/false) setCoalescing(true/false) combine CDATA sections with surrounding text? combine CDATA sections with surrounding text? setExpandEntityReferences(true/false) n Further DocumentBuilderFactory configuration methods to control the form of the resulting DOM tree:

14 SDPL 2003Notes 3: XML Processor Interfaces14 JAXP Transformation API n earlier 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 »gives an identity transformation, which simply copies the Source to the Result

15 SDPL 2003Notes 3: XML Processor Interfaces15 XSLT JAXP: Using Transformers (1).newTransformer(…).transform(.,.)

16 SDPL 2003Notes 3: XML Processor Interfaces16 JAXP Transformation Packages javax.xml.transform: javax.xml.transform: –Classes Transformer and TransformerFactory ; initialization similar to parsers and parser factories n Transformation Source object can be –a DOM tree, a SAX XMLReader or an input stream n Transformation Result object can be –a DOM tree, a SAX ContentHandler or an output stream

17 SDPL 2003Notes 3: XML Processor Interfaces17 Source-Result combinations XMLReader (SAX Parser) Transformer DOM ContentHandler InputStream OutputStream DOMSourceResult

18 SDPL 2003Notes 3: XML Processor Interfaces18 JAXP Transformation Packages (2) n Classes to create Source and Result objects from DOM, SAX and I/O streams defined in packages –javax.xml.transform.dom, javax.xml.transform.sax, and javax.xml.transform.stream n An identity transformation from a DOM Document to I/O stream gives a vendor- neutral way to serialize DOM documents –(the only option in JAXP)

19 SDPL 2003Notes 3: XML Processor Interfaces19 Serializing a DOM Document as XML text n Identity transformation to an output stream: TransformerFactory tFactory = TransformerFactory.newInstance(); // Create an identity transformer: Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(myDOMdoc); StreamResult result = new StreamResult(System.out); transformer.transform(source, result);

20 SDPL 2003Notes 3: XML Processor Interfaces20 Controlling the form of the result? As above, but create a Transformer with XSLT instructions as a StreamSource, say saveSpecSrc : As above, but create a Transformer with XSLT instructions as a StreamSource, say saveSpecSrc : <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:transform> // Now create a tailored transformer: Transformer transformer = tFactory.newTransformer(saveSpecSrc);

21 SDPL 2003Notes 3: XML Processor Interfaces21 Other Java APIs for XML n JDOM –a Java-specific variant of W3C DOM –http://www.jdom.org/ DOM4J ( http://www.dom4j.org/ ) DOM4J ( http://www.dom4j.org/ ) –roughly similar to JDOM; richer set of features: –powerful navigation with integrated XPath support n JAXB (Java Architecture for XML Binding) –compiles DTDs to DTD-specific classes for reading, manipulating and writing valid documents –http://java.sun.com/xml/jaxb/

22 SDPL 2003Notes 3: XML Processor Interfaces22 JAXP: Summary n An interface for using XML Processors –SAX/DOM parsers, XSLT transformers n Supports plugability of XML processors n Defines means to control parsing and handling of parse errors (through SAX ErrorHandlers) n Defines means to write out DOM Documents n Included in JDK 1.4


Download ppt "SDPL 2003Notes 3: XML Processor Interfaces1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –A Java-based answer: through."

Similar presentations


Ads by Google