XML DOM Get Node Values

XML DOM Get Node Values

The nodeValue property is used to get the text value of a node.

The getAttribute() method returns the value of an attribute.


Get the Value of an Element

In the DOM, everything is a node. Element nodes does not have a text value.

The text of an element node is stored in a child node. This node is called a text node.

The way to get the text of an element, is to get the value of the child node (text node).


Get an Element Value

The getElementsByTagName() method returns a node list containing all elements with the specified tag name in the same order as they appear in the source document.

The following code loads "books.xml" into xmlDoc using loadXMLDoc() and retrieves the first <title> element:

xmlDoc=loadXMLDoc("books.xml");

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

The childNodes property returns a list of child nodes. The <title> element has only one child node. It is a text node.

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

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

The nodeValue property returns the text value of the text node:

Example

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

Try it yourself »

Result:  txt = "Everyday Italian"

Loop through all <title> elements: Try it yourself


Get the Value of an Attribute

In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.

The way to get the value of an attribute, is to get its text value.

This can be done using the getAttribute() method or using the nodeValue property of the attribute node.


Get an Attribute Value - getAttribute()

The getAttribute() method returns an attribute value.

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

Example

xmlDoc=loadXMLDoc("books.xml");

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

Try it yourself »

Result:  txt = "en"

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Set the txt variable to be the value of the "lang" attribute of the first title element node

Loop through all <book> elements and get their "category" attributes: Try it yourself


Get an Attribute Value - getAttributeNode()

The getAttributeNode() method returns an attribute node.

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

Example

xmlDoc=loadXMLDoc("books.xml");

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

Try it yourself »

Result:  txt = "en"

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Get the "lang" attribute node of the first <title> element node
  3. Set the txt variable to be the value of the attribute

Loop through all <book> elements and get their "category" attributes: Try it yourself

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值