May 27, 2021 XML DOM
1. XML DOM gets the node value
2. Gets the value of the element
4. Gets the value of the property
The nodeValue property is used to get the text value of the node.
The getAttribute() method returns the value of the property.
In doM, each component is a node. The element node does not have a text value.
The text of the element node is stored in the child node. This node is called a text node.
The way to get element text is to get the value of this child node (text node).
The getElementsByTagName() method returns a list of nodes that contain all the elements that have the specified label name, in the order in which they appear in the source document.
The following code loads "books.xml" into xmlDoc by using loadXMLDoc() and retrieves the first element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
The childNodes property returns a list of child nodes. T he element has only one child node. It is a text node.
The following code retrieves the text nodes of the element:
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
The nodeValue property returns the text value of the text node:
RESULTS: txt - "Everyday Italian"
Traversing all the elements: Try
In DOM, properties are also nodes. Unlike element nodes, property nodes have text values.
The way to get the value of a property is to get its text value.
You can do this by using the getAttribute() method or the nodeValue property of the property node.
The getAttribute() method returns the property value.
The following code retrieves the text value of the "lang" property of the first element:
Result: txt s "en"
Example explanation:
Traverse all the elements and get their "category" property:
Try it
The getAttributeNode() method returns the property node.
The following code retrieves the text value of the "lang" property of the first element:
Result: Result: txt s "en"
Example explanation:
Traversing all the elements and getting their "category" properties: Try it
That's how doM gets node values, and in the next section, continue to learn how to change node values.