May 27, 2021 XML
3. Gets the value of the element
4. Gets the value of the property
Earlier in this tutorial, we covered XML DOM and used XML DOM's getElementsByTagName() method to get data back from XML documents.
In this chapter we'll combine some other important XML DOM methods.
You can learn more about XML DOM in our XML DOM tutorial.
The XML file used in the following example: .xml.
The following example retrieves the text value of the first element:
The following example retrieves the text value of the "lang" property of the first element:
The following example changes the text value of the first element:
XML DOM's setAttribute() method can be used to change an existing property value or create a new property.
The following example creates a new property (edition-"first") and then adds it to each element:
XML DOM's createElement() method creates a new element node.
XML DOM's createTextNode() method creates a new text node.
XML DOM's appendChild() method adds child nodes to the node (after the last child node).
To create a new element with text content, you need to create both a new element node and a new text node, and then append it to the existing node.
The following example creates a new element with the following text: First, and then adds it to the first element:
The instance explanation
The following instance removes the first node of the first element:
Note: The results of the above instance may vary depending on the browser used. F irefox treats new line characters as empty text nodes, which is not the case with Internet Explorer. You can read more about this problem and how to avoid it in our XML DOM tutorial.