Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.

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.
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.
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.
XML Parsers By Chongbing Liu. XML Parsers  What is a XML parser?  DOM and SAX parser API  Xerces-J parsers overview  Work with XML parsers (example)
1 SAX and more… CS , Spring 2008/9. 2 SAX Parser SAX = Simple API for XML XML is read sequentially When a parsing event happens, the parser invokes.
SAX A parser for XML Documents. XML Parsers What is an XML parser? –Software that reads and parses XML –Passes data to the invoking application –The application.
Tomcat Java and XML. Announcements  Final homework assigned Wednesday  Two week deadline  Will cover servlets + JAXP.
Xerces The Apache XML Project Yvonne Yao. Introduction Set of libraries that provides functionalities to parse XML documents Set of libraries that provides.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
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.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Chapter 13 XML Concept of XML Simple Example of XML XML vs. HTML in Syntax XML Structure DTD and CDATA Sections Concept of SAX Processing Download and.
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.
Sheet 1XML Technology in E-Commerce 2001Lecture 4 XML Technology in E-Commerce Lecture 4 Case Study: XmlMessenger.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 05: XML Professor Chen Li Department of Computer Science UC Irvine CS122BNotes 05:
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.
Sheet 1XML Technology in E-Commerce 2001Lecture 6 XML Technology in E-Commerce Lecture 6 XPointer, XSLT.
The Joy of SAX (and DOM, and JDOM…) Bill MacCartney 11 October 2004.
XML Anisha K J Jerrin Thomas. Outline  Introduction  Structure of an XML Page  Well-formed & Valid XML Documents  DTD – Elements, Attributes, Entities.
SDPL 2003Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
1 Document Object Model (DOM) MV4920 – XML 24 September 2001 Simon R. Goerger MAJ, US Army
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.
SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.
Advanced Java Session 9 New York University School of Continuing and Professional Studies.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
3/29/2001 O'Reilly Java Java API for XML Processing 1.1 What’s New Edwin Goei Engineer, Sun Microsystems.
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.
Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)
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.
Sheet 1XML Technology in E-Commerce 2001Lecture 2 XML Technology in E-Commerce Lecture 2 Logical and Physical Structure, Validity, DTD, XML Schema.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
XML Study-Session: Part III
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.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
Schema Data Processing
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.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
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
Week-9 (Lecture-1) XML DTD (Data Type Document): An XML document with correct syntax is called "Well Formed". An XML document validated against a DTD is.
XML. Contents  Parsing an XML Document  Validating XML Documents.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
In this session, you will learn to:
Parsing XML into programming languages
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
XML Parsers By Chongbing Liu.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Java API for XML Processing
A parser for XML Documents
SAX2 29-Jul-19.
Presentation transcript:

Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX

Sheet 2XML Technology in E-Commerce 2001Lecture 3 General Model for XML Processing Document Object Model (DOM) –Logical Model; –DOM Interfaces; –Example; Simple API for XML (SAX); –Parser Architecture; –Events; –Java Classes and Interfaces; –Example. Error Handling; Summary; Lecture Outline

Sheet 3XML Technology in E-Commerce 2001Lecture 3 General Model for XML Processing DOM - Document Object Model SAX - Simple API for XML

Sheet 4XML Technology in E-Commerce 2001Lecture 3 DOM DOM defines: –Logical model for XML documents; –Platform and language independent application programming interfaces for model manipulation; DOM allows: –accessing document content; –modifying document content; –creating new documents in the memory; DOM homepage: –

Sheet 5XML Technology in E-Commerce 2001Lecture 3 XML Document: John Smith Derek Warwick Mik Douglas img competition resultsphotos imgname John SmithD. WarwickM. Douglas DOM Logical Model DOM Tree Structure: XML Document is a set of Nodes that form tree structure. There are different node types: for elements, attributes, text content, etc.

Sheet 6XML Technology in E-Commerce 2001Lecture 3 DOM Interfaces are defined in the Interface Definition Language (IDL); There are bindings for different languages: DOM Programming Interfaces IDL JavaScriptC++PythonJava

Sheet 7XML Technology in E-Commerce 2001Lecture 3 DOM Interface Hierarchy More important interfaces defined in Java package org.w3c.dom

Sheet 8XML Technology in E-Commerce 2001Lecture 3 DOM Structure Model

Sheet 9XML Technology in E-Commerce 2001Lecture 3 DOM provides two groups of interfaces: –Generic: Node, NodeList, NamedNodeMap; –Specialized: Node subinterfaces for elements, attributes, text nodes, etc. Interfaces: –Node - Deitel 8.5, Fig. 8.7, page 201; –Document - Deitel 8.5, Fig. 8.5, page 200; –Element - Deitel 8.5, Fig Page 200; –Attr; –Text; DOM Interface Details

Sheet 10XML Technology in E-Commerce 2001Lecture 3 Demo - Example on fig. 8.10, Deitel 8.5, page 202; Tools: –Java 1.2.2; –Java API for XML Processing (JAXP) 1.0.1; Classes: jaxp.jar and parser.jar; Demo files: –ReplaceText.java; –MyErrorHandler.java; –intro.xml; DOM Demo

Sheet 11XML Technology in E-Commerce 2001Lecture 3 Importing packages: import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import com.sun.xml.tree.XmlDocument; Instantiation of the parser. DOM does not specify parser instantiation, so this is an implementation specific detail: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating( true ); DocumentBuilder builder = factory.newDocumentBuilder(); DOM Demo Explained (1)

Sheet 12XML Technology in E-Commerce 2001Lecture 3 Loading and Parsing the XML file: Document document = builder.parse(new File( "intro.xml")); Getting the root element (myMessage): Node root = document.getDocumentElement(); Casting the root to Element type: Element myMessageNode = ( Element ) root; Finding the message elements : NodeList messageNodes = myMessageNode.getElementsByTagName("message"); Getting the first message element: Node message = messageNodes.item(0); DOM Demo Explained (2)

Sheet 13XML Technology in E-Commerce 2001Lecture 3 Creating a new text content and replacing the old one: Text newText = document.createTextNode("New Changed Message!!"); Text oldText = (Text) message.getChildNodes().item(0); message.replaceChild( newText, oldText ); Writing the changed document to a new file. DOM does not specify how to save the DOM structure. This is implementation specific detail: ((XmlDocument) document).write( new FileOutputStream("intro1.xml")); DOM Demo Explained (3)

Sheet 14XML Technology in E-Commerce 2001Lecture 3 DOM level 1 (Discussed here); DOM level 2: –Namespace support; –Stylesheets interface; –Model for events; –Views, Range and Traversal interfaces; DOM level 3 (work in progress): –Loading and Saving documents; –Model for DTD and Schema; DOM Specification Levels

Sheet 15XML Technology in E-Commerce 2001Lecture 3 General Model for XML Processing Document Object Model (DOM) –Logical Model; –DOM Interfaces; –Example; Simple API for XML (SAX); –Parser Architecture; –Events; –Java Classes and Interfaces; –Example. Error Handling; Summary; Lecture Outline

Sheet 16XML Technology in E-Commerce 2001Lecture 3 SAX - Simple API for XML; Developed by the members of XML-DEV list in 1998; SAX is Event based: –The parser reports parsing events: start and end of the document, start and end of an element, errors, etc. –When an event occurs, the parser invokes a method on an event handler; –The application handles the events accordingly; SAX home page: SAX

Sheet 17XML Technology in E-Commerce 2001Lecture 3 XML Source SAX Parser Document Handler Error Handler DTD Handler Entity Resolver Application SAX Parser Architecture DocumentHandler, ErrorHandler, DTDHandler and EntityResolver are interfaces that the Application can implement

Sheet 18XML Technology in E-Commerce 2001Lecture 3 SAX Events

Sheet 19XML Technology in E-Commerce 2001Lecture 3 SAX DocumentHandler Interface Java package org.xml.sax; DocumentHandler Interface; More important methods: public abstract void startDocument() public abstract void endDocument() public abstract void startElement(String name, AttributeList atts) public abstract void endElement(String name) public abstract void characters(char ch[],int start, int length) public abstract void processingInstruction(String target,String data)

Sheet 20XML Technology in E-Commerce 2001Lecture 3 SAX Demo Demo - Example on fig. 9.3, Deitel 9.6, page 235; Tools: –Java 1.2.2; –Java API for XML Processing (JAXP) 1.0.1; Classes: jaxp.jar and parser.jar; Demo files: –Tree.java; –Sample XML documents;

Sheet 21XML Technology in E-Commerce 2001Lecture 3 SAX Demo Explained (1) Importing packages: import org.xml.sax.*; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; Class HandlerBase: –Provide default implementation of the four event handlers. Applications usually extends it and overrides some methods: public class Tree extends HandlerBase –Tree class overrides the methods from DocumentHandler interface; –Registration in the parser before parsing;

Sheet 22XML Technology in E-Commerce 2001Lecture 3 SAX Demo Explained (2) Factory Instantiation: SAXParserFactory saxFactory = SAXParserFactory.newInstance(); saxFactory.setValidating( validate ); Obtaining the parser and start parsing: SAXParser saxParser = saxFactory.newSAXParser(); saxParser.parse(new File(args[1]), new Tree());

Sheet 23XML Technology in E-Commerce 2001Lecture 3 SAX Error Handling Three error types: –Fatal errors: usually violation of well-formedness constraints. The parser must stop processing; –Errors: usually violation of validity rules; –Warnings: related to DTD; Errors are handled by implementing ErrorHandler Interface; The Tree class overrides the default implementation of methods for warnings and errors; The same mechanism is used with DOM parsers;

Sheet 24XML Technology in E-Commerce 2001Lecture 3 Main Changes: –Namespace support; –Introduction of Filter mechanism; –Interface DocumentHandler is replaced by ContentHandler; –New exception classes; SAX 2.0

Sheet 25XML Technology in E-Commerce 2001Lecture 3 SAX and DOM Comparison DOM: –maintains an internal structure for the document; –possible high memory usage for large documents; –enables traversing; SAX: –doesn’t maintain an internal structure; –enables building of custom structure; –low memory usage; –usually faster than DOM; –traversing is impossible without internal structure; Usually a DOM implementation is built on the top of a SAX parser;

Sheet 26XML Technology in E-Commerce 2001Lecture 3 Two approaches for XML processing: –Tree-based (DOM); –Event-based (SAX); Tools: –JDK 1.2.2; –JAXP (used in the book); –JAXP 1.1 is also available; –See also Read: Deitel 8, 9 Assignment: Modify the case study in Deitel 8.8. In the new version the query should be based only on year, month and day (time is excluded). Add new functionality for making new appointment for a meeting on the found day and at specified time. For more detailed explanation and some hints see the course site. Summary