XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302

Slides:



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

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.
14-Jun-15 DOM. 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.
©Silberschatz, Korth and Sudarshan10.1Database System Concepts W3C Activities HTML: is the lingua franca for publishing on the Web XHTML: an XML application.
1 XML and Data Management XML Processors Hachim Haddouti Al Akhawayn University SSE
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.
Java API for XML Processing (JAXP) CSE 4/586: Distributed Systems Department of Computer Science and Engineering University at Buffalo, New York Jia Zhao.
1 Processing XML with Java Representation and Management of Data on the Internet A comprehensive tutorial about XML processing with JavaXML processing.
1 Processing XML with Java CS , Spring 2008/9.
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
1 Processing XML with Java Dr. Praveen Madiraju Modified from Dr.Sagiv ’ s slides.
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
XML Anisha K J Jerrin Thomas. Outline  Introduction  Structure of an XML Page  Well-formed & Valid XML Documents  DTD – Elements, Attributes, Entities.
Lecture 7 of Advanced Databases XML Querying & Transformation Instructor: Mr.Ahmed Al Astal.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
Structured-Document Processing Languages Spring 2011 Course Review Repetitio mater studiorum est!
Lecture 6 of Advanced Databases XML Querying & Transformation Instructor: Mr.Eyad Almassri.
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.
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.
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
XML 6.4 DOM 6. The XML ‘Alphabet Soup’ XMLExtensible Markup Language Defines XML documents XSLExtensible Stylesheet Language Language for expressing stylesheets;
Intro to XML Originally Presented by Clifford Lemoine Modified by Box.
Computing & Information Sciences Kansas State University Thursday, 15 Mar 2007CIS 560: Database System Concepts Lecture 24 of 42 Thursday, 15 March 2007.
Extensible MarkUp Language. AGENDA  OVERVIEW OF XML  DATA TYPE DEFINITION LANGUAGE  XML SCHEMA  XML PARSERS 1) DOM PARSER 2) SAX PARSER 3) JAXB PARSER.
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 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
1 Processing XML with Java Modified from Dr.Sagiv ’ s slides.
1 4/13/01 CSE 121/131 Programming Spring 2001 Lecture Notes 7  A. Sahuguet & V.Tannen.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
XML Study-Session: Part III
CS 157B: Database Management Systems II February 11 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
SNU OOPSLA Lab. DOM/SAX Applications The ubiquitous XML(9) © copyright 2001 SNU OOPSLA Lab.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
SAX2 and DOM2 Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
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.
What is XML? eXtensible Markup Language eXtensible Markup Language A subset of SGML (Standard Generalized Markup Language) A subset of SGML (Standard Generalized.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Computing & Information Sciences Kansas State University Friday, 20 Oct 2006CIS 560: Database System Concepts Lecture 24 of 42 Friday, 20 October 2006.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
18-Feb-16 XML eXtensible Markup Language THETOPPERSWAY.COM.
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.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
D Copyright © 2004, Oracle. All rights reserved. Using Oracle XML Developer’s Kit.
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.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
Java API for XML Processing
Parsing with SAX using Java Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Java XML IS
{ 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
DOM 8-Dec-18.
A parser for XML Documents
Presentation transcript:

XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA IV. XML Parsing APIs December 2005

Outline  Introduction  XML Basics  XML Structural Constraint Specification  Document Type Definitions (DTDs)  XML Schema  XML/Database Mappings  XML Parsing APIs  Simple API for XML (SAX)  Document Object Model (DOM)  XML Querying and Transformation  XPath  XQuery  XSLT  XML Applications

Parsers What is a parser? –A program that analyses the grammatical structure of an input, with respect to a given formal grammar –The parser determines how a sentence can be constructed from the grammar of the language by describing the atomic elements of the input and the relationship among them

XML-Parsing Standards We will consider two parsing methods that implement W3C standards for accessing XML SAX (Simple API for XML) –event-driven parsing –“serial access” protocol –Read only API DOM (Document Object Model) –convert XML into a tree of objects –“random access” protocol –Can update XML document (insert/delete nodes)

SAX Parser SAX = Simple API for XML XML is read sequentially When a parsing event happens, the parser invokes the corresponding method of the corresponding handler The handlers are programmer’s implementation of standard Java API (i.e., interfaces and classes) Similar to an I/O-Stream, goes in one direction

Sample Data DEC Orders Data in XML: several orders, each with several items each item has a part number and a quantity

Sample Data DEC Orders Data in XML: several orders, each with several items each item has a part number and a quantity startDocument endDocument Parsing Event

Sample Data DEC Orders Data in XML: several orders, each with several items each item has a part number and a quantity startElement endElement

Sample Data DEC Orders Data in XML: several orders, each with several items each item has a part number and a quantity characters

SAX Parsers SAX Parser When you see the start of the document do … When you see the start of an element do … When you see the end of an element do ….

Used to create a SAX Parser Handles document events: start tag, end tag, etc. Handles Parser Errors Handles DTD Handles Entities

SAX API Two important classes in the SAX API : SAXParser and HandlerBase. A new SAXParser object is created by: public SAXParser() Register a SAX handler with the parser object to receive notifications of various parser events by: public void setDocumentHandler(DocumentHandler h) A similar method is available to register an error handler: public void setErrorHandler(ErrorHandler h)

SAX API - Continued The HandlerBase class is a default base class for all handlers. It implements the default behavior for the various handlers. Application writers extend this class by rewriting the following event handler methods: public void startDocument() throws SAXException public void endDocument() throws SAXException public void startElement() throws SAXException public void endElement() throws SAXException public void characters() throws SAXException public void warning() throws SAXException public void error() throws SAXException

Creating a SAX Parser import org.xml.sax.*; import oracle.xml.parser.v2.SAXParser; public class SampleApp extends HandlerBase { // Global variables declared here static public void main(String [] args){ Parser parser = new SAXParser(); SampleApp app = new SampleApp(); parser.setDocumentHandler(app); parser.setErrorHandler(app); try { parser.parse(createURL(args[0]).toString()); } catch (SAXParseException e) { System.out.println(e.getMessage()); }

SAX API – Sample Application Write a SAX Parser that reads the orders.xml file and extracts the various data items and creates SQL insert statements to insert the data into a relational database table. //Global Variables Vector itemNum = new Vector(); int numberOfRows, numberOfItems; String elementEncountered, orderNumber, takenBy, customer, receivedDate, partNumber, quantity; //elementEncountered holds most recent element name public void startDocument() throws SAXException { //Print SQL comment, initialize variable System.out.println("--Start of SQL insert Statements"); itemNum.setSize(1); numberOfRows = 0; }//end startDocument

SAX API – Sample Application continued public void startElement(String name, AttributeList atts) throws SAXException { elementEncountered = name; if (name.equalsIgnoreCase("items")) { numberOfItems = 0; }//end if statement }//end startElement public void characters(char[] cbuf, int start, int len) { if (elementEncountered.equals("orderNumber") orderNumber = new String(cbuf,start,len); else if(elementEncountered.equals("takenBy")) { takenBy = new String(cbuf,start,len);... }//end characters

SAX API – Sample Application continued public void endElement(String name) throws SAXException{ if (name.equalsIgnoreCase("item")) { numberOfItems++; if (numberOfItems == 1) { // first item; create orders row System.out.println( "insert into orders values('"+ orderNumber +"','"+ customer +"','"+ takenBy +"','"+ receivedDate +"','null');"); } System.out.println("insert into odetails values('"+ orderNumber +"','"+ partNumber +"','"+ quantity +"');"); }//end if statement if (name.equalsIgnoreCase("items")) { System.out.println(" "); } }//end endElement

SAX API – Sample Application continued public void endDocument(){ System.out.println("End of SQL insert statements."); }//end endDocument public void warning(SAXParseException e) throws SAXException { System.out.println("Warning:"+ e.getMessage()); } public void error(SAXParseException e) throws SAXException{ throw new SAXException(e.getMessage()); }

DOM Parser DOM = Document Object Model Parser creates a tree object out of the document User accesses data by traversing the tree –The tree and its traversal conform to a W3C standard The API allows for constructing, accessing and manipulating the structure and content of XML documents

Israel 6,199,008 Jerusalem Ashdod France 60,424,213

The DOM Tree

Using a DOM Tree DOM Parser DOM Tree XML File APIAPI Application

The Node Interface  The nodes of the DOM tree include  a special root (denoted document)  element nodes  text nodes and CDATA sections  attributes  comments  and more...  Every node in the DOM tree implements the Node interface

Node Navigation  Every node has a specific location in tree  Node interface specifies methods for tree navigation  Node getFirstChild();  Node getLastChild();  Node getNextSibling();  Node getPreviousSibling();  Node getParentNode();  NodeList getChildNodes();  NamedNodeMap getAttributes()

Node Navigation (cont) getFirstChild() getPreviousSibling() getChildNodes() getNextSibling() getLastChild() getParentNode()

GA Georgia Peach State ATL Atlanta... DOM Parsing Example Consider the following XML data file describing geographical information about states in U.S.

<!ELEMENT state (scode, sname, capital, citiesin*, nickname, population)> Geography XML Data DTD

create type city_type as object ( ccode varchar2(15), cname varchar2(50) ); create type cities_in_table as table of city_type; create table state ( scode varchar2(15), sname varchar2(50), nickname varchar2(100), population number(30), capital city_type, cities_in cities_in_table, primary key (scode)) nested table cities_in store as cities_tab; Geography Data: SQL Schema (Oracle Objects)

import org.w3c.dom.*; import org.w3c.dom.Node; import oracle.xml.parser.v2.*; public class CreateGeoData { static public void main(String[] argv) throws SQLException { // Get an instance of the parser DOMParser parser = new DOMParser(); // Set various parser options: validation on, // warnings shown, error stream set to stderr. parser.setErrorStream(System.err); parser.setValidationMode(true); parser.showWarnings(true); // Parse the document. parser.parse(url); // Obtain the document. XMLDocument doc = parser.getDocument(); Create DOM Parser Object

NodeList sl = doc.getElementsByTagName("state"); NodeList cl = doc.getElementsByTagName("city"); XMLNode e = (XMLNode) sl.item(j); scode = e.valueOf("scode"); sname = e.valueOf("sname"); nickname = e.valueOf("nickname"); population = Long.parseLong(e.valueOf("population")); XMLNode child = (XMLNode) e.getFirstChild(); while (child != null) { if (child.getNodeName().equals("capital")) break; child = (XMLNode) child.getNextSibling(); } NamedNodeMap nnm = child.getAttributes(); XMLNode n = (XMLNode) nnm.item(0); Traverse DOM Tree

Node Manipulation Children of a node in a DOM tree can be manipulated - added, edited, deleted, moved, copied, etc. To constructs new nodes, use the methods of Document –createElement, createAttribute, createTextNode, createCDATASection etc. To manipulate a node, use the methods of Node –appendChild, insertBefore, removeChild, replaceChild, setNodeValue, cloneNode(boolean deep) etc.

SAX vs DOM Parsing: Efficiency  The DOM object built by DOM parsers is usually complicated and requires more memory storage than the XML file itself  A lot of time is spent on construction before use  For some very large documents, this may be impractical  SAX parsers store only local information that is encountered during the serial traversal  Hence, programming with SAX parsers is, in general, more efficient

Programming using SAX is Difficult  In some cases, programming with SAX is difficult:  How can we find, using a SAX parser, elements e1 with ancestor e2?  How can we find, using a SAX parser, elements e1 that have a descendant element e2?  How can we find the element e1 referenced by the IDREF attribute of e2?

Node Navigation  SAX parsers do not provide access to elements other than the one currently visited in the serial (DFS) traversal of the document  In particular,  They do not read backwards  They do not enable access to elements by ID or name  DOM parsers enable any traversal method  Hence, using DOM parsers is usually more comfortable

More DOM Advantages  DOM object  compiled XML  You can save time and effort if you send and receive DOM objects instead of XML files  But, DOM object are generally larger than the source  DOM parsers provide a natural integration of XML reading and manipulating  e.g., “cut and paste” of XML fragments

Which should we use? DOM vs. SAX If your document is very large and you only need to extract only a few elements – use SAX If you need to manipulate (i.e., change) the XML – use DOM If you need to access the XML many times – use DOM (assuming the file is not too large)