Working with Elements and Attributes Using DOM Eugenia Fernandez IUPUI.

Slides:



Advertisements
Similar presentations
HTML DOM Part-II. Create Node We can create a node in JavaScript dynamically. There are 3 types of Node –Comment –Element –Text Node We can also create.
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
XML CS What is XML?  XML: a "skeleton" for creating markup languages  you already know it!  syntax is identical to XHTML's: content  languages.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Document Object Model (DOM) JavaScript manipulation of the DOM.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science University of Kentucky.
1 Extensible Markup Language: XML HTML: portable, widely supported protocol for describing how to format data XML: portable, widely supported protocol.
1 XML and Data Management XML Processors Hachim Haddouti Al Akhawayn University SSE
XSL Transformations Lecture 8, 07/08/02. Templates The whole element is a template The match pattern determines where this template applies Result element(s)
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.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
1 Processing XML with Java CS , Spring 2008/9.
1 XML Data Management 4. Domain Object Model Werner Nutt.
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JS: Document Object Model (DOM)
5 Processing XML Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Posting XML Data From the Client to a Server Eugenia Fernandez IUPUI.
JavaScript & DOM Client-side scripting. JavaScript JavaScript is a tool to automate client side (which is implemented using HTML so far) JavaSript syntax.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
Parsing with DOM using MSXML Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 4 - XML.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
XML DOM Functionality in.NET DSK Chakravarthy
XQL, OQL and SQL Xia Tang Sixin Qian Shijun Shen Feb 18, 2000.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms.
JavaScript IV ECT 270 Robin Burke. Outline DOM JS document model review W3C DOM.
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.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
Markup basics. Markup languages add commentary to text files –so that the commentary can be ignored if not understood eg HyperText Markup Language –adds.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
XML DOM.
XML and Object Serialization. Structure of an XML Document Header Root Element Start Tags / End Tags Element Contents – Child Elements – Text – Both (mixed.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
Advanced DOM Builds on last presentation on DOM Allows you to dynamically create elements and position them on a page DOM methods/properties are W3C standard.
Document Object Model.  The XML DOM (Document Object Model) defines a standard way for accessing and manipulating XML documents.  The DOM presents an.
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.
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:
Introduction to JavaScript DOM Instructor: Sergey Goldman.
THE DOM.
Scripting the DOM MIS 3502, Fall 2016 Jeremy Shafer Department of MIS
Introduction to the Document Object Model
DOM Robin Burke ECT 360.
Binary search tree. Removing a node
CS3220 Web and Internet Programming Client-Side JavaScript and jQuery
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
DOM Document Object Model.
DOM Document Object Model.
Processing XML.
In this session, you will learn to:
Document Object Model (DOM): Objects and Collections
XML DOM and CSS Instructors: Geoffrey Fox and Bryan Carpenter
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Working with Elements and Attributes Using DOM Eugenia Fernandez IUPUI

Accessing Element Nodes The DOM provides a variety of ways to find elements, attributes and other node types Search for element by tag name Search for nodes using an XPath pattern Access nodes in a collection

Search by TagName Search for elements in the document by tag name doc.getElementsByTagName(“author”) returns a collection of “author” nodes Search an element for child nodes by tag name set book = rootNode.firstChild set bkauthors = book.getElementByTagName(“author”) returns a node list of all descendants of the first book element of type “author”

Search by XPath Pattern Use selectNodes method with an XPath pattern to return a NodeList collection of all nodes matching the pattern set cheapbooks = rootNode.selectNodes(“//book[price<10]”) selectSingleNode method works the same, but returns only the first node that matches the pattern

Accessing Nodes in a Collection set doc = booksdso.XMLDocument set authors = doc.getElementByTagName(“author”) for each author in authors ‘process data in author node next

Finding Tag Name of an Element use the nodeName property inherited from the Node object anyElement.nodeName use the tagName property defined in the Element object anyElement.tagName

Accessing Element Content The text content of an element is held in a child Text node. To access the content of an element you must use the nodeValue property of the Text node. node nodeName: price nodeValue: null Text node nodeName: #text nodeValue:

Creating a New Element You use the createElement and createTextNode methods of the Document object to create a new element. You create the node and then insert it in the appropriate position in the tree.

Example: Creating a Quantity Element 1. Create a Text node and assign to it the value of the new element set quantityText = doc.createTextNode(“2”) 2. Create an Element node set quantityElement = doc.createElement(“quantity”) 3. Add the Text node as a child of the new Elment Node quantityElement.appendChild quantityText 4. Insert the new quantity Element node at the right place in the XML document (use appendChild or insertBefore methods) set orderItemNode = doc.selectSingleNode(“//orderitem”) orderItemNode.appendChild quantityElement 34.99

Removing an Element You use the removeChild method of the Element object to delete an element. First locate the element you want to remove and then use removeChild with the parent of that element set bookNode = doc.selectSingleNode(“//book”) bookNode.parentNode.removeChild bookNode

Replacing an Element You use the replaceChild method of the Element object to replace an element. First locate the element you want to replace and then use replaceChild to replace it with another element. set bookNode = doc.selectSingleNode(“//book”) set oldAuthor = bookNode.selectSingleNode(“author”) bookNode.replaceChild oldAuthor, newAuthor

Attribute Nodes Represented by Attr nodes, which support the nodeType, nodeName, and nodeValue inherited from the Node object. The Attr object defines additional properties name – gets or sets the name of an attribute value – gets or sets the value of an attribute node nodeName: book nodeValue: null Attr node name: title value: XML by Example

Accessing Attributes You access attributes through the element to which they belong getAttribute method returns the value of an attribute setAttribute method assigns eht value of an attribute set booknode = doc.selectSingleNode(“//book”) MsgBox booknode.getAttribute(“title”) set booknode = doc.selectSingleNode(“//book”) booknode.setAttribute “title”, “XML in a Nutshell”

The Attributes Property The Element object has an attributes property that returns a NamedNodeMap object that contains al the attributes of the element. The NamedNodeMap is a collection of name-value pairs. You can use a loop to access the attributes in the attributes collection or you can use the getNamedItem and setNamedItem methods of the NamedNodeMap to get and set the value of a specific attribute in the collection.

Using the Attributes Property Via a loop Using getNamedItem MsgBox bookNode.attributes.getNamedItem(“title”).value set attrList = book.attributes for each attr in attrList MsgBox attr.name & “ “ & attr.value Next

Creating an Attribute You can use the setAttribute method to define a new attribute. You could also use the createAttribute method of the Document object, set its value, and then add it to the element using the setAttributeNode method of the Element object. set bookNode = doc.selectSingleNode(“//book”) bookNode.setAttribute “isbn” “ ” set attrNode = doc.createAttribute(“isbn”) attrNode.value = “ ” bookNode.setAttributeNode attrNode

Removing an Attribute You can use the removeAttribute method of the Element object to delete an attribute. You could also use the removeAttributeNode method of the Element object, after you locate the attribute node using the getAttributeNode method. set bookNode = doc.selectSingleNode(“//book”) bookNode.removeAttribute “isbn” set attrNode = bookNode.getAttributeNode (“isbn”) bookNode.removeAttributeNode attrNode

The End