SDPL 20063.2: Document Object Model1 n How to provide uniform access to structured documents in parsers, browsers, editors, databases,...? - DOM, and how.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
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.
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.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
The Document Object Model (DOM) & Asynchronous Javascript And XML (AJAX) : an introduction UFCEKG-20-2 : Data, Schemas and Applications.
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
1 XML Data Management 4. Domain Object Model Werner Nutt.
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
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!
XML Data Processing and Transformation ดร. มารุต บูรณรัช : หัวข้อพิเศษด้านเทคโนโลยีสารสนเทศขั้นสูง.
SDPL 2002Notes 3.2: Document Object Model1 XML 접근과 관련된 자료.
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Lecture 2 : Understanding the Document Object Model (DOM) UFCFR Advanced Topics in Web Development II 2014/15 SHAPE Hong Kong.
Adminisztratív November 24.-én az előadás és a gyakorlat elmarad A nagy gyakorlati feladatok beszedése a december 1.-én tartandó gyakorlatra tolódik A.
17 Apr 2002 XML Programming - DOM Andy Clark. DOM Design Premise Derived from browser document model Defined in IDL – Lowest common denominator programming.
Structured-Document Processing Languages Spring 2005 Course Review Repetitio mater studiorum est!
SDPL : Document Object Model1 n How access structured documents uniformly in parsers, browsers, editors, databases,...? n Overview of the W3C DOM.
SDPL 2002Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
XML Parsers Overview  Types of parsers  Using XML parsers  SAX  DOM  DOM versus SAX  Products  Conclusion.
SDPLNotes 3.3: DOM Examples1 3.3 DOM Examples 1. DOM processing in a Web browser –a toy "XML database browser" –with JavaScript and MS Internet Explorer.
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;
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extensible MarkUp Language. AGENDA  OVERVIEW OF XML  DATA TYPE DEFINITION LANGUAGE  XML SCHEMA  XML PARSERS 1) DOM PARSER 2) SAX PARSER 3) JAXB PARSER.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
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.
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.
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
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.
SDPLNotes 3.2: DOM1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications (parsers, browsers,
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Structured-Document Processing Languages Spring 2007 Course Review Repetitio mater studiorum est!
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 2002Notes 3.2: Document Object Model1 The Misconceived Web n The original vision of the WWW was as a hyperlinked document-retrieval system. n It did.
Document Object Model (DOM). Outline  Introduction of DOM  Overview of DOM  DOM Relationships  Standard DOM.
SDPL 20063: XML Processor Interfaces1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
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.
XML DOM Week 11 Web site:
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.
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 Parsers Overview Types of parsers Using XML parsers SAX DOM
DOM Robin Burke ECT 360.
Java/XML.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
DOM Document Object Model.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
DOM 8-Dec-18.
DOM 24-Feb-19.
XML DOM and CSS Instructors: Geoffrey Fox and Bryan Carpenter
XML Programming in Java
Presentation transcript:

SDPL : Document Object Model1 n How to provide uniform access to structured documents in parsers, browsers, editors, databases,...? - DOM, and how to use it n Selective overview of the W3C DOM Spec –second in “XML-family” of Recs »Level 1, W3C Rec, Oct »Level 2, W3C Rec, Nov »Level 3 (of 21 modules!) work-in-progress; Validation, Core, and Load and Save Recommendations (Spring 2004) 3.2 Document Object Model (DOM)

SDPL : Document Object Model2 DOM: What is it? n An object-based, language-neutral API for XML and HTML documents –Allows programs and scripts to build, access, and modify documents –Supports the development of querying, filtering, transformation, formatting etc. applications on top of DOM implementations n In contrast to “Serial Access XML” could think as “Directly Obtainable in Memory”

SDPL : Document Object Model3 DOM structure model n Based on O-O concepts: –methods (to access or change object’s state) –interfaces (declaration of a set of methods) –objects (encapsulation of data and methods) n Roughly similar to the XSLT/XPath data model (to be discussed later)  syntax-tree –Tree structure implied by abstract relationships defined by the API; Data structures of an implementation may differ (but hardly do(?))

SDPL : Document Object Model4 <invoice form="00" type="estimated"> type="estimated"> John Doe John Doe Pyynpolku 1 Pyynpolku KUOPIO KUOPIO DOM structure model invoice name addressdata address form="00"type="estimated" John Doe streetaddresspostoffice KUOPIO Pyynpolku 1... Document Element NamedNodeMap Text

SDPL : Document Object Model5 Structure of DOM Level 1 I: DOM Core Interfaces –Fundamental interfaces »basic interfaces: Document, Element, Attr, Text,... –"Extended" (XML specific) interfaces »CDATASection, DocumentType, Notation, Entity, EntityReference, ProcessingInstruction II: DOM HTML Interfaces –more convenient access to HTML documents –(we'll ignore these)

SDPL : Document Object Model6 DOM Level 2 –Level 1: basic representation and manipulation of document structure and content (No access to the contents of a DTD) n DOM Level 2 adds –support for namespaces –accessing elements by ID attribute values –optional features (we’ll skip these) »interfaces to document views and style sheets »an event model (for, say, user actions on elements) »methods for traversing the document tree and manipulating regions of document (e.g., selected by the user of an editor) –Load/Save of documents not specified (until Level 3)

SDPL : Document Object Model7 DOM Language Bindings n Language-independence: –DOM interfaces are defined using OMG Interface Definition Language (IDL; Defined in Corba Specification) n Language bindings (implementations of interfaces) defined in the Recommendation for –Java (See the Java API doc) and –ECMAScript (standardised JavaScript)

SDPL : Document Object Model8 Core Interfaces: Node & its variants Node Comment DocumentFragmentAttr Text Element CDATASection ProcessingInstruction CharacterData EntityDocumentTypeNotation EntityReference “Extendedinterfaces” Document

SDPL : Document Object Model9 DOM interfaces: Node invoice name addressdata address form="00"type="estimatedbill" John Doe streetaddresspostoffice KUOPIO Pyynpolku 1 Node getNodeType, getNodeName, getNodeValue getOwnerDocumentgetParentNode hasChildNodes, getChildNodes getFirstChild, getLastChild getPreviousSibling, getNextSibling hasAttributes, getAttributes appendChild(newChild)insertBefore(newChild,refChild)replaceChild(newChild,oldChild)removeChild(oldChild)Document Element NamedNodeMap Text...

SDPL : Document Object Model10 Type and Name of a Node node.getNodeType() : short int constants 1, 2, …, 12 for Node.ELEMENT_NODE, Node.ATTRIBUTE_NODE, Node.TEXT_NODE, … node.getNodeType() : short int constants 1, 2, …, 12 for Node.ELEMENT_NODE, Node.ATTRIBUTE_NODE, Node.TEXT_NODE, … n node.getNodeName() –for an Element = node.getTagName() –for an Attr: the name of the attribute –for anonymous nodes : "#text", "#document", "#comment" etc

SDPL : Document Object Model11 The Value of a Node node.getNodeValue() node.getNodeValue() –content of a text node, value of attribute, …; null for an Element (!!) –(in XSLT/XPath the value of a node is its full textual content) –DOM 3 gives access to full textual content with the method node.getTextContent()

SDPL : Document Object Model12 Object Creation in DOM Each DOM Node n lives in the scope of a Document: n.getOwnerDocument() Each DOM Node n lives in the scope of a Document: n.getOwnerDocument() Objects implementing interface X are created by factory methods doc.create X (…), where doc is a Document object. E.g: Objects implementing interface X are created by factory methods doc.create X (…), where doc is a Document object. E.g: doc.createElement("A"), doc.createAttribute("href"), doc.createTextNode("Hello!") Loading and saving of Document s is left implementation-specific Loading and saving of Document s is left implementation-specific

SDPL : Document Object Model13 invoice name addressdata address form="00"type="estimated" John Doe streetaddresspostoffice KUOPIO Pyynpolku 1 DocumentgetDocumentElementgetElementById(IdVal)getElementsByTagName(tagName)createElement(tagName)createTextNode(data) Node DOM interfaces: Document Document Element NamedNodeMap Text...

SDPL : Document Object Model14 DOM interfaces: Element invoice invoicepage name addressee addressdata address form="00"type="estimatedbill" John Doe streetaddresspostoffice KUOPIO Pyynpolku 1 ElementgetTagName()hasAttribute(name)getAttribute(name) setAttribute(attrName, value) removeAttribute(name)getElementsByTagName(name) Node Document Element NamedNodeMap Text

SDPL : Document Object Model15 Text Content Manipulation in DOM for an object c that implements the CharacterData interface (Text, Comments, CDATASections) : for an object c that implements the CharacterData interface (Text, Comments, CDATASections) : –c.substringData( offset, count ) –c.appendData( string ) –c.insertData( offset, string ) –c.deleteData( offset, count ) –c.replaceData( offset, count, string ) ( = c.deleteData(offset, count); c.insertData(offset, string) )

SDPL : Document Object Model16 Additional Core Interfaces (1) NodeList for ordered lists of nodes NodeList for ordered lists of nodes –e.g. from Node.getChildNodes() or Element.getElementsByTagName("name") »all descendant elements of type "name" in document order ( "*" matches any element type) Accessing a specific node, or iterating over all nodes of a NodeList : Accessing a specific node, or iterating over all nodes of a NodeList : –E.g., to process all children of node : for (i=0; i<node.getChildNodes().getLength(); i++) process(node.getChildNodes().item(i));

SDPL : Document Object Model17 Additional Core Interfaces (2) NamedNodeMap for unordered sets of nodes accessed by their name: NamedNodeMap for unordered sets of nodes accessed by their name: –e.g. from Node.getAttributes() NodeList s and NamedNodeMap s are "live": NodeList s and NamedNodeMap s are "live": –updates of the document structure are reflected to their contents –e.g., this would delete every other child of node n : NodeList cList = n.getChildNodes(); for (i=0; i<cList.getLength(); i++) n.removeChild(cList.item(i)); –e.g., this would delete every other child of node n : NodeList cList = n.getChildNodes(); for (i=0; i<cList.getLength(); i++) n.removeChild(cList.item(i)); »That’s strange! (What happens?)

SDPL : Document Object Model18 DOM: XML Implementations n Java-based parsers e.g. Apache Xerces, Apache Crimson, … n In MS IE browser: COM programming interfaces for C/C++ and Visual Basic; ActiveX object programming interfaces for script languages Perl: XML::DOM (Implements DOM Level 1) Perl: XML::DOM (Implements DOM Level 1) n Others? APIs for other applications than parsers? –Vendors of different kinds of systems have participated in the W3C DOM WG

SDPL : Document Object Model19 A Java-DOM Example Command-line tool RegListMgr for maintaining a course registration list Command-line tool RegListMgr for maintaining a course registration list –with single-letter commands for listing, adding, updating and deleting student records Example: Example: $ java RegListMgr reglist.xml Document loaded succesfully > … 40: Tero Ulvinen, TKM1, 2 41: heli viinikainen, tkt5, 1 list the contents l

SDPL : Document Object Model20 Registration list: the XML file Juho Ahopelto Juho Ahopelto TKT4 TKT </reglist>

SDPL : Document Object Model21 Registration List: the DTD

SDPL : Document Object Model22 Loading and Saving the RegList Loading of the registration list into DOM Document doc implemented with a JAXP DocumentBuilder Loading of the registration list into DOM Document doc implemented with a JAXP DocumentBuilder –assume this has been done: doc is a handle to the Document Saving implemented with a JAXP Transformer Saving implemented with a JAXP Transformer n … to be discussed later

SDPL : Document Object Model23 Listing student records (1) NodeList students = doc.getElementsByTagName("student"); for (int i=0; i<students.getLength(); i++) for (int i=0; i<students.getLength(); i++) showStudent((Element) students.item(i)); showStudent((Element) students.item(i)); private void showStudent(Element student) { // Collect relevant sub-elements: // Collect relevant sub-elements: Node given = student.getElementsByTagName("given").item(0); Node given = student.getElementsByTagName("given").item(0); Node family = given.getNextSibling(); Node family = given.getNextSibling(); Node bAndY = student. getElementsByTagName("branchAndYear").item(0); Node bAndY = student. getElementsByTagName("branchAndYear").item(0); Node = bAndY.getNextSibling(); Node = bAndY.getNextSibling(); Node group = .getNextSibling(); Node group = .getNextSibling();

SDPL : Document Object Model24 Listing student records (2) // Method showStudent continues: // Method showStudent continues: System.out.print( student.getAttribute("id").substring(3)); System.out.print( student.getAttribute("id").substring(3)); System.out.print(": " + given.getFirstChild().getNodeValue() ); System.out.print(": " + given.getFirstChild().getNodeValue() ); //.. similarly access and display the // value of family, bAndY, , and group // … //.. similarly access and display the // value of family, bAndY, , and group // … } // showStudent

SDPL : Document Object Model25 Adding New Records Example: Example: First name (or to finish): > a … 41: heli viinikainen, tkt5, 1 42: Antti Ahkera, tkt3, 2 add students Antti Last name: AhkeraBranch&year: Finished adding records > tkt3 First name (or to finish): 2l

SDPL : Document Object Model26 Implementing addition of records (1) Element rootElem = doc.getDocumentElement(); Element rootElem = doc.getDocumentElement(); String lastID = rootElem.getAttribute("lastID"); String lastID = rootElem.getAttribute("lastID"); int lastIDnum = java.lang.Integer.parseInt(lastID); int lastIDnum = java.lang.Integer.parseInt(lastID); System.out.print( "First name (or to finish): "); System.out.print( "First name (or to finish): "); String firstName = terminalReader.readLine().trim(); String firstName = terminalReader.readLine().trim(); while (firstName.length() > 0) { while (firstName.length() > 0) { // Get the next unused ID: // Get the next unused ID: ID = "RDK" + new Integer(++lastIDnum).toString(); ID = "RDK" + new Integer(++lastIDnum).toString(); // … Read values lastName, bAndY, , // … Read values lastName, bAndY, , // and group from the terminal, and then... // and group from the terminal, and then...

SDPL : Document Object Model27 Implementing addition of records (2) Element newStudent = newStudent(doc, ID, firstName, lastName, bAndY, , group); Element newStudent = newStudent(doc, ID, firstName, lastName, bAndY, , group); rootElem.appendChild(newStudent); rootElem.appendChild(newStudent); System.out.print( "First name (or to finish): "); System.out.print( "First name (or to finish): "); firstName = readLine(); firstName = readLine(); } // while firstName.length() > 0 // Update the last ID used: String newLastID = java.lang.Integer.toString(lastIDnum); rootElem.setAttribute("lastID", newLastID); System.out.println("Finished adding records");

SDPL : Document Object Model28 Creating new student records (1) private Element newStudent(Document doc, String ID, String fName, String lName, String bAndY, String , String grp) { private Element newStudent(Document doc, String ID, String fName, String lName, String bAndY, String , String grp) { Element stu = doc.createElement("student"); Element stu = doc.createElement("student"); stu.setAttribute("id", ID); stu.setAttribute("id", ID); Element newName = doc.createElement("name"); Element newName = doc.createElement("name"); Element newGiven = doc.createElement("given"); newGiven.appendChild(doc.createTextNode(fName)); Element newGiven = doc.createElement("given"); newGiven.appendChild(doc.createTextNode(fName)); Element newFamily = doc.createElement("family"); Element newFamily = doc.createElement("family"); newFamily.appendChild(doc.createTextNode(lName)); newFamily.appendChild(doc.createTextNode(lName)); newName.appendChild(newGiven); newName.appendChild(newGiven); newName.appendChild(newFamily); stu.appendChild(newName); newName.appendChild(newFamily); stu.appendChild(newName);

SDPL : Document Object Model29 Creating new student records (2) // method newStudent(…) continues: Element newBr = doc.createElement("branchAndYear"); // method newStudent(…) continues: Element newBr = doc.createElement("branchAndYear"); newBr.appendChild(doc.createTextNode(bAndY)); newBr.appendChild(doc.createTextNode(bAndY)); stu.appendChild(newBr); stu.appendChild(newBr); Element new = doc.createElement(" "); Element new = doc.createElement(" "); new .appendChild(doc.createTextNode( )); new .appendChild(doc.createTextNode( )); stu.appendChild(new ); stu.appendChild(new ); Element newGrp = doc.createElement("group"); Element newGrp = doc.createElement("group"); newGrp.appendChild(doc.createTextNode(group)); newGrp.appendChild(doc.createTextNode(group)); stu.appendChild(newGrp); stu.appendChild(newGrp); return stu; return stu; } // newStudent

SDPL : Document Object Model30 Updates and Deletions n Updates and deletions implemented similarly, by manipulating the DOM structures n To be treated in the exercises

SDPL : Document Object Model31 Summary of XML APIs so far n Give applications access to the structure and contents of XML documents n Event-based APIs (e.g. SAX) –notify application through parsing events –efficient n Object-model (or tree) based APIs (e.g. DOM) –provide a full parse tree –more convenient, but require much resources with large documents n Major parsers support both SAX and DOM –used through proprietary methods –used through JAXP (  next)