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.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Advertisements

The Java Platform and XML Portable Code, Portable Data James Duncan Davidson Staff Engineer, Sun Microsystems, Inc.
Multi-Model Digital Video Library Professor: Michael Lyu Member: Jacky Ma Joan Chung Multi-Model Digital Video Library LYU9904 Multi-Model Digital Video.
XML Parsing Using Java APIs AIP Independence project Fall 2010.
SDPL 2002Notes 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 Transformation Package and Xalan Extensions 黃立昇
Parsing XML into programming languages JAXP, DOM, SAX, JDOM/DOM4J, Xerces, Xalan, JAXB.
31 Signs That Technology Has Taken Over Your Life: #6. When you go into a computer store, you eavesdrop on a salesperson talking with customers -- and.
Xerces The Apache XML Project Yvonne Yao. Introduction Set of libraries that provides functionalities to parse XML documents Set of libraries that provides.
21-Jun-15 SAX (Abbreviated). 2 XML Parsers SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard.
26-Jun-15 SAX. SAX and DOM SAX and DOM are standards for XML parsers--program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
MC365 XML Parsers. Today We Will Cover: An overview of the Java API’s used for XML processing Creating an XML document in Java Parsing an XML document.
JAX- Java APIs for XML by J. Pearce. Some XML Standards Basic –SAX (sequential access parser) –DOM (random access parser) –XSL (XSLT, XPATH) –DTD Schema.
XML: Java Dr Andy Evans. Java and XML Couple of things we might want to do: Parse/write data as XML. Load and save objects as XML. We’ll mainly discuss.
17 Apr 2002 XML Programming: TrAX Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
SDPL : (XML APIs) JAXP1 3.3 JAXP: Java API for XML Processing n How can applications use XML processors? –In Java: through JAXP –An overview of.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
17 Apr 2002 XML Programming: JAXP Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.
The Joy of SAX (and DOM, and JDOM…) Bill MacCartney 11 October 2004.
SDPL 2003Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
1 Document Object Model (DOM) MV4920 – XML 24 September 2001 Simon R. Goerger MAJ, US Army
Structured-Document Processing Languages Spring 2011 Course Review Repetitio mater studiorum est!
Java API for XML Processing (JAXP) توسط : محمّدمهدي حامد استاد راهنما : دكتر مسعود رهگذر.
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.
Structured-Document Processing Languages Spring 2005 Course Review Repetitio mater studiorum est!
Advanced Java Session 9 New York University School of Continuing and Professional Studies.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 JAXP & XSLT. Objectives 2  TrAX API  Transforming XML Documents  Workshops.
3/29/2001 O'Reilly Java Java API for XML Processing 1.1 What’s New Edwin Goei Engineer, Sun Microsystems.
1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.
EXtensible Markup Language (XML) James Atlas July 15, 2008.
SDPL 2002Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
SDPL 20113: XML APIs and SAX1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
XML Parsers Overview  Types of parsers  Using XML parsers  SAX  DOM  DOM versus SAX  Products  Conclusion.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
SDPL 2002Notes 3.2: Document Object Model1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
Web Services with Apache CXF Part 2: JAXB and WSDL to Java Robert Thornton.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
XML Study-Session: Part III
Document Object Model DOM. Agenda l Introduction to DOM l Java API for XML Parsing (JAXP) l Installation and setup l Steps for DOM parsing l Example –Representing.
Java and XML. What is XML XML stands for eXtensible Markup Language. A markup language is used to provide information about a document. Tags are added.
COSC617 Project XML Tools Mark Liu Sanjay Srivastava Junping Zhang.
Structured-Document Processing Languages Spring 2007 Course Review Repetitio mater studiorum est!
Dom and XSLT Dom – document object model DOM – collection of nodes in a tree.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Structured-Document Processing Languages Spring 2004 Course Review Repetitio mater studiorum est!
Web services. DOM parsing and SOAP.. Summary. ● Exercise: SAX-Based checkInvoice(), ● push parsing, ● event-based parsing, ● traversal order is depth-first.
SDPL 20063: XML Processor Interfaces1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
SDPL 2001Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How applications can manipulate structured documents? –An overview of document parser.
1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops.
1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.
Java API for XML Processing
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Parsing XML into programming languages
Using XML Tools CS551 – Fall 2001.
Java/XML.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Java API for XML Processing
WaysInJavaToParseXML
Presentation transcript:

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 /3_apis.html, from which also some graphics are borrowed]

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

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

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

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

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();

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

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);

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

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);

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?

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)

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:

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

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

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

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

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)

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);

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 " indent="yes" <xsl:output encoding="ISO " indent="yes" doctype-system="reglist.dtd" /> doctype-system="reglist.dtd" /> </xsl:transform> // Now create a tailored transformer: Transformer transformer = tFactory.newTransformer(saveSpecSrc);

SDPL 2003Notes 3: XML Processor Interfaces21 Other Java APIs for XML n JDOM –a Java-specific variant of W3C DOM – DOM4J ( ) DOM4J ( ) –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 –

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