1 / 18

Working with Elements and Attributes Using DOM

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.

Télécharger la présentation

Working with Elements and Attributes Using DOM

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Elements and Attributes Using DOM Eugenia Fernandez IUPUI

  2. 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

  3. 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”

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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.

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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”

  14. 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.

  15. 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

  16. 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

  17. 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

  18. The End

More Related