180 likes | 278 Views
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.
E N D
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 <price>9.99</price> • 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. <price> node nodeName: price nodeValue: null Text node nodeName: #text nodeValue: 9.99
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 <orderitem title=“XML by Example”> <price>34.99</price></orderitem> • Create a Text node and assign to it the value of the new element set quantityText = doc.createTextNode(“2”) • Create an Element node set quantityElement = doc.createElement(“quantity”) • Add the Text node as a child of the new Elment Node quantityElement.appendChild quantityText • 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
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 <book title=“XML by Example”/> <book> 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 set attrList = book.attributesfor each attr in attrList MsgBox attr.name & “ “ & attr.valueNext MsgBox bookNode.attributes.getNamedItem(“title”).value
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” “234-536-984059” set attrNode = doc.createAttribute(“isbn”) attrNode.value = “234-536-984059” 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