Download presentation
Presentation is loading. Please wait.
Published byStella Porter Modified over 9 years ago
1
SDPL 20063.2: 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. 1998 »Level 2, W3C Rec, Nov. 2000 »Level 3 (of 21 modules!) work-in-progress; Validation, Core, and Load and Save Recommendations (Spring 2004) 3.2 Document Object Model (DOM)
2
SDPL 20063.2: 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”
3
SDPL 20063.2: 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(?))
4
SDPL 20063.2: Document Object Model4 <invoice form="00" type="estimated"> type="estimated"> John Doe John Doe Pyynpolku 1 Pyynpolku 1 70460 KUOPIO 70460 KUOPIO...... DOM structure model invoice name addressdata address form="00"type="estimated" John Doe streetaddresspostoffice 70460 KUOPIO Pyynpolku 1... Document Element NamedNodeMap Text
5
SDPL 20063.2: 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)
6
SDPL 20063.2: 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)
7
SDPL 20063.2: 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)
8
SDPL 20063.2: Document Object Model8 Core Interfaces: Node & its variants Node Comment DocumentFragmentAttr Text Element CDATASection ProcessingInstruction CharacterData EntityDocumentTypeNotation EntityReference “Extendedinterfaces” Document
9
SDPL 20063.2: Document Object Model9 DOM interfaces: Node invoice name addressdata address form="00"type="estimatedbill" John Doe streetaddresspostoffice 70460 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...
10
SDPL 20063.2: 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
11
SDPL 20063.2: 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()
12
SDPL 20063.2: 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
13
SDPL 20063.2: Document Object Model13 invoice name addressdata address form="00"type="estimated" John Doe streetaddresspostoffice 70460 KUOPIO Pyynpolku 1 DocumentgetDocumentElementgetElementById(IdVal)getElementsByTagName(tagName)createElement(tagName)createTextNode(data) Node DOM interfaces: Document Document Element NamedNodeMap Text...
14
SDPL 20063.2: Document Object Model14 DOM interfaces: Element invoice invoicepage name addressee addressdata address form="00"type="estimatedbill" John Doe streetaddresspostoffice 70460 KUOPIO Pyynpolku 1 ElementgetTagName()hasAttribute(name)getAttribute(name) setAttribute(attrName, value) removeAttribute(name)getElementsByTagName(name) Node Document Element NamedNodeMap Text
15
SDPL 20063.2: 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) )
16
SDPL 20063.2: 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));
17
SDPL 20063.2: 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?)
18
SDPL 20063.2: 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
19
SDPL 20063.2: 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, tero@fake.addr.fi, 2 41: heli viinikainen, tkt5, heli@fake.addr.fi, 1 list the contents l
20
SDPL 20063.2: Document Object Model20 Registration list: the XML file Juho Ahopelto Juho Ahopelto TKT4 TKT4 juho@fake.addr.fi juho@fake.addr.fi 2 2 </reglist>
21
SDPL 20063.2: Document Object Model21 Registration List: the DTD
22
SDPL 20063.2: 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
23
SDPL 20063.2: 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 email = bAndY.getNextSibling(); Node email = bAndY.getNextSibling(); Node group = email.getNextSibling(); Node group = email.getNextSibling();
24
SDPL 20063.2: 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, email, and group // … //.. similarly access and display the // value of family, bAndY, email, and group // … } // showStudent
25
SDPL 20063.2: Document Object Model25 Adding New Records Example: Example: First name (or to finish): > a … 41: heli viinikainen, tkt5, heli@fake.addr.fi, 1 42: Antti Ahkera, tkt3, antti@fake.addr.fi, 2 add students Antti Last name: AhkeraBranch&year: Finished adding records > email:tkt3 group:antti@fake.addr.fi First name (or to finish): 2l
26
SDPL 20063.2: 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, email, // … Read values lastName, bAndY, email, // and group from the terminal, and then... // and group from the terminal, and then...
27
SDPL 20063.2: Document Object Model27 Implementing addition of records (2) Element newStudent = newStudent(doc, ID, firstName, lastName, bAndY, email, group); Element newStudent = newStudent(doc, ID, firstName, lastName, bAndY, email, 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");
28
SDPL 20063.2: Document Object Model28 Creating new student records (1) private Element newStudent(Document doc, String ID, String fName, String lName, String bAndY, String email, String grp) { private Element newStudent(Document doc, String ID, String fName, String lName, String bAndY, String email, 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);
29
SDPL 20063.2: 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 newEmail = doc.createElement("email"); Element newEmail = doc.createElement("email"); newEmail.appendChild(doc.createTextNode(email)); newEmail.appendChild(doc.createTextNode(email)); stu.appendChild(newEmail); stu.appendChild(newEmail); 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
30
SDPL 20063.2: Document Object Model30 Updates and Deletions n Updates and deletions implemented similarly, by manipulating the DOM structures n To be treated in the exercises
31
SDPL 20063.2: 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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.