17 Apr 2002 XML Programming - DOM Andy Clark. DOM Design Premise Derived from browser document model Defined in IDL – Lowest common denominator programming.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Advertisements

XML IV. The Document Object Model The Document Object model is a hierarchical structure of an XML document. It provides a means for accessing, and manipulating.
The Document Object Model
Document Object Model. Lecture 18 The Document Object Model (DOM) is not a programming language It is an object-oriented model of web documents Each.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science University of Kentucky.
11-Jun-15 More DOM. Manipulating DOM trees DOM, unlike SAX, gives you the ability to create and modify XML trees There are a few roadblocks along the.
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.
1 XML and Data Management XML Processors Hachim Haddouti Al Akhawayn University SSE
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science CSC 560: Management of XML Data.
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.
1 Processing XML with Java Representation and Management of Data on the Internet.
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.
SE 5145 – eXtensible Markup Language (XML ) DOM (Document Object Model) (Part I) /Spring, Bahçeşehir University, Istanbul.
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
C# The new language for Updated by Pavel Ježek © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
1 XML Data Management 4. Domain Object Model Werner Nutt.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
5 Processing XML Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.
Java WWW Week 10 Version 2.1 Mar 2008 Slide Java (JSP) and XML  Format of lecture: What is XML? A sample XML file… How to use.
Introduction to XPath Transparency No. 1 Introduction to XPath API Cheng-Chia Chen.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
April 30, ICE 1341 – Programming Languages (Lecture #18) In-Young Ko Programming Languages (ICE 1341) Lecture #18 Programming Languages (ICE 1341)
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.
Consuming eXtensible Markup Language (XML) feeds.
Consuming eXtensible Markup Language (XML) feeds.
DOM Programming The Document Object Model standardises  what an application can see of the XML data  how it can access it An XML structure is a tree.
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
SNU OOPSLA Lab. DOM/SAX Applications The ubiquitous XML(9) © copyright 2001 SNU OOPSLA Lab.
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.
SDPLNotes 3.2: DOM1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications (parsers, browsers,
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
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.
1 JAXP & XPATH. Objectives 2  XPath  JAXP Processing of XPath  Workshops.
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
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.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
Web services. DOM parsing and SOAP.. Summary. ● Exercise: SAX-Based checkInvoice(), ● push parsing, ● event-based parsing, ● traversal order is depth-first.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
Processing of structured documents Part 4. XML processing model zXML processor is used to read XML documents and provide access to their content and structure.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
JS: Document Object Model (DOM) DOM stands for Document Object Model, and allows programmers generic access to: DOM stands for Document Object Model, and.
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.
USING ANDROID WITH THE DOM. Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation.
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
XML. Contents  Parsing an XML Document  Validating XML Documents.
DOM Robin Burke ECT 360.
Java/XML.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
More DOM 13-Nov-18.
DOM Document Object Model.
Jagdish Gangolly State University of New York at Albany
More DOM 28-Nov-18.
DOM 8-Dec-18.
Document Object Model (DOM): Objects and Collections
More DOM.
DOM 24-Feb-19.
XML DOM and CSS Instructors: Geoffrey Fox and Bryan Carpenter
XML Programming in Java
Presentation transcript:

17 Apr 2002 XML Programming - DOM Andy Clark

DOM Design Premise Derived from browser document model Defined in IDL – Lowest common denominator programming lang. Most methods packed into base Node object Re-invents basic collection interfaces Factory model – Allows custom classes to be used for nodes Separation between required and optional modules

Document Object Model Generic tree model – Node Type, name, value Attributes Parent node Previous, next sibling nodes First, last child nodes – “Live” Collections Lists Maps Node Parent Prev. SiblingNext Sibling First ChildLast Child

Node Interfaces Package org.w3c.dom – Document – Element, Attr, Text – ProcessingInstruction, Comment – EntityReference, CDATASection – DocumentType, Notation, Entity – DocumentFragment – Note: All nodes descend from org.w3c.dom.Node.

org.w3c.dom.Node (1 of 4) Node information – short getNodeType(); e.g. Node.ELEMENT_NODE – String getNodeName(); – String getNodeValue(); Namespace information – String getPrefix(); – String getLocalName(); – String getNamespaceURI();

org.w3c.dom.Node (2 of 4) Attributes – boolean hasAttribute(String name); – NamedNodeMap getAttributes(); Children – Node getFirstChild(); – Node getLastChild(); – boolean hasChildNodes(); – NodeList getChildNodes();

org.w3c.dom.Node (3 of 4) Links to other nodes – Document getOwnerDocument(); – Node getParentNode(); – Node getPreviousSibling(); – Node getNextSibling(); Other methods – Node cloneNode(boolean deep);

org.w3c.dom.Node (4 of 4) Editing node values – void setNodeValue(String value); – void setPrefix(String prefix); Editing tree structure – Node appendChild(Node child); – Node insertBefore(Node newChild, Node refChild); – Node removeChild(Node child); – Node replaceChild(Node newChild, Node oldChild);

org.w3c.dom.NodeList Methods – int getLength(); – Node item(int index);

org.w3c.dom.NamedNodeMap (1 of 2) Query methods – int getLength(); – Node item(int index); – Node getNamedItem(String name); – Node getNamedItemNS(String namespaceURI, String localName);

org.w3c.dom.NamedNodeMap (2 of 2) Editing methods – Node setNamedItem(Node node); – Node setNamedItemNS(Node node); – Node removeNamedItem(String name); – Node removeNamedItemNS(String namespaceURI, String localName);

Parsing a Document Instantiate parser – org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser(); Parse file and query document – parser.parse(“document.xml”); – org.w3c.dom.Document document = parser.getDocument(); – Note: Should use JAXP to instantiate parser.

Traversing a Document (1 of 8) Recursively: method #1 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 if (node.hasChildNodes()) { 04 NodeList children = node.getChildNodes(); 05 for (int i = 0; i < children.getLength(); i++) { 06 Node child = children.item(i); 07 traverse(child); 08 } 09 } 10}

Traversing a Document (2 of 8) Recursively: method #1 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 if (node.hasChildNodes()) { 04 NodeList children = node.getChildNodes(); 05 for (int i = 0; i < children.getLength(); i++) { 06 Node child = children.item(i); 07 traverse(child); 08 } 09 } 10}

Traversing a Document (3 of 8) Recursively: method #1 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 if (node.hasChildNodes()) { 04 NodeList children = node.getChildNodes(); 05 for (int i = 0; i < children.getLength(); i++) { 06 Node child = children.item(i); 07 traverse(child); 08 } 09 } 10}

Traversing a Document (4 of 8) Recursively: method #1 – Note: Avoid calling geLength() in loop but remember that DOM collections are live! 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 if (node.hasChildNodes()) { 04 NodeList children = node.getChildNodes(); 05 for (int i = 0; i < children.getLength(); i++) { 06 Node child = children.item(i); 07 traverse(child); 08 } 09 } 10}

Traversing a Document (5 of 8) Recursively: method #2 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 Node child = node.getFirstChild(); 04 while (child != null) { 05 traverse(child); 06 child = child.getNextSibling(); 07 } 08}

Traversing a Document (6 of 8) Recursively: method #2 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 Node child = node.getFirstChild(); 04 while (child != null) { 05 traverse(child); 06 child = child.getNextSibling(); 07 } 08}

Traversing a Document (7 of 8) Recursively: method #2 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 Node child = node.getFirstChild(); 04 while (child != null) { 05 traverse(child); 06 child = child.getNextSibling(); 07 } 08}

Traversing a Document (8 of 8) Recursively: method #2 – Note: Be sure to call nextSibling() on the child object, not the node object. 01public void traverse(Node node) { 02 System.out.println(“node: “+node.getNodeName()); 03 Node child = node.getFirstChild(); 04 while (child != null) { 05 traverse(child); 06 child = child.getNextSibling(); 07 } 08}

org.w3c.dom.Document (1 of 3) Extends org.w3c.dom.Node Query methods – DocumentType getDoctype(); – Element getDocumentElement(); – Element getElementById(String id); – NodeList getElementsByTagName(String name); – NodeList getElementsByTagNameNS(String nsURI, String local);

org.w3c.dom.Document (2 of 3) Important factory methods – Element createElement(String name); – Element createElementNS(String namespaceURI, String qualifiedName); – Attr createAttribute(String name); – Attr createAttributeNS(String namespaceURI, String qualifiedName); – Text createTextNode(String data);

org.w3c.dom.Document (3 of 3) Other factory methods – ProcessingInstruction createProcessingInstruction(String target, String data); – Comment createComment(String data); – CDATASection createCDATASection(String data); – EntityReference createEntityReference(String name); – DocumentFragment createDocumentFragment(); Missing factory methods – DocumentType, Notation, Entity

Building a Tree Programmatically Instantiate Document object – org.w3c.dom.Document document = new org.apache.xerces.dom.DocumentImpl(); Create content – Element root = document.createElement(“root”); – document.appendChild(root); – Text text = document.createTextNode(“text”); – root.appendChild(text); – Note: Should use JAXP to instantiate document.

Searching for Elements (1 of 4) Document#getElementsByTagName 01public void iteratePeople(Document document) { 02 NodeList people = document.getElementsByTagName(“person”); 03 int length = people.getLength(); 04 for (int i = 0; i < length; i++) { 05 Node person = people.item(i); 06 // do something with 07 } 08}

Searching for Elements (2 of 4) Document#getElementsByTagName 01public void iteratePeople(Document document) { 02 NodeList people = document.getElementsByTagName(“person”); 03 int length = people.getLength(); 04 for (int i = 0; i < length; i++) { 05 Node person = people.item(i); 06 // do something with 07 } 08}

Searching for Elements (3 of 4) Document#getElementsByTagName 01public void iteratePeople(Document document) { 02 NodeList people = document.getElementsByTagName(“person”); 03 int length = people.getLength(); 04 for (int i = 0; i < length; i++) { 05 Node person = people.item(i); 06 // do something with 07 } 08}

Searching for Elements (4 of 4) Document#getElementsByTagName 01public void iteratePeople(Document document) { 02 NodeList people = document.getElementsByTagName(“person”); 03 int length = people.getLength(); 04 for (int i = 0; i < length; i++) { 05 Node person = people.item(i); 06 // do something with 07 } 08}

Importing Nodes (1 of 2) The problem – Moving nodes from different documents, even if same implementation, throws a DOM exception – Why? Factory design pattern allows different implementations and custom node implementations Therefore, cannot simply move nodes to another tree The solution – Document#importNode – Document#adoptNode – DOM Level 3 working draft

Importing Nodes (2 of 2) Document#importNode – WRONG! – RIGHT 01Document doc1, doc2; 02Node node1, parent2; 03parent2.appendChild(node1); 01Document doc1, doc2; 02Node node1, parent2; 03Node node2 = doc2.importNode(node1); 04parent2.appendChild(node2);

Common Reactions (1 of 2) Why use interfaces? Classes would be better! – Factory design pattern Removes dependency on specific implementation Allows multiple implementations Allows factory to return custom data objects Why are all these methods on Node interface? – Other languages don’t have RTTI – Avoids type casting (no, the cast is not free)

Common Reactions (2 of 2) DOM sucks! “xyz” is so much better! – Cons, actual and perceived Designed by committee Steeper learning curve than “xyz” Not as easy to use as “xyz” – Pros Allows custom implementations Standard tree model for XML documents

Advanced Topics – for another time DOM Level 2 – Events – Traversal Tree walkers and iterators – Ranges Selection DOM Level 3 – working draft – Load and Save / Abstract Schemas – XPath

Useful Links DOM Level 1 Specification – DOM Level 2 Specifications – – – – –

XML Programming: DOM Andy Clark