Latest web development tutorials

XML DOM - Get node value

nodeValue property is used to get the text node values.

getAttribute () method returns the value of the property.


Gets the value of the element

In the DOM, everything is a node. No text element node values.

Element node text is stored in the child node. This node is called a text node.

Get text of an element, it is to get the value of the child node (text node).


Get element values

getElementsByTagName () method returns a list of nodes that contain all of the elements have the specified tag name, the order in which the element is the order they appear in the source document.

The following code by using loadXMLDoc () to " the Books.xml " into xmlDoc and retrieve the first <title> element:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0];

childNodes attribute returns a list of child nodes. <Title> element has only one child node. It is a text node.

The following code retrieves the text node <title> element:

x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];

nodeValue property returns the text node values:

Examples

x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
txt=y.nodeValue;

try it"

Results: txt = "Everyday Italian"

Through all <title> elements: Try


Gets the value of the property

In the DOM, the property is also a node. Unlike element nodes, attribute nodes have text values.

Gets the value of the property of the method is to get its text value.

By using getAttribute nodeValue attribute () method or attribute node to accomplish this task.


Get property values ​​- getAttribute ()

getAttribute () method returns the propertyvalue.

The following code retrieves the first text value "lang" attribute of the <title> element:

Examples

xmlDoc=loadXMLDoc("books.xml");

txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");

try it"

Results: txt = "en"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Txt variable to the value of the first title element node "lang" attribute

Through all <book> elements and get their "category" attributes: Try


Gets the property value - getAttributeNode ()

getAttributeNode () method returnsan attribute node.

The following code retrieves the first text value "lang" attribute of the <title> element:

Examples

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");
txt=x.nodeValue;

try it"

Results: Result: txt = "en"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Get the first <title> "lang" attribute nodes element node
  3. Txt variable to the value of the property

Through all <book> elements and get their "category" attributes: Try