Tutorials 参考 练习 Menu
Paid Courses

XML 教程

XML主页 XML简介 XML使用方法 XML Tree XML语法 XML元素 XML属性 XML命名空间 XML显示 XML HttpRequest XML解析器 XML DOM XML XPath XML XSLT XML XQuery XML XLink XML验证器 XML DTD XML模式 XML服务器 XML 示例 XML 测试 XML 证书

XML AJAX

AJAX简介 AJAX XMLHttp AJAX请求 AJAX响应 AJAX XML文件 AJAX PHP AJAX ASP AJAX数据库 AJAX应用程序 AJAX 示例

XML DOM

DOM简介归纳 DOM节点 DOM访问 DOM节点信息 DOM节点列表 DOM遍历 DOM导航 DOM获取价值 DOM变更节点 DOM删除节点 DOM替换节点 DOM创建节点 DOM添加节点 DOM克隆节点 DOM 示例

XPath 教程

XPath简介 XPath节点 XPath语法 XPath轴 XPath运算子 XPath 示例

XSLT 教程

XSLT简介 XSL语言 XSLT转换 XSLT <template> XSLT <value-of> XSLT <for-each> XSLT <sort> XSLT <if> XSLT <choose> XSLT申请 客户端上的XSLT 服务器上的XSLT XSLT编辑XML XSLT 示例

XQuery 教程

XQuery简介 XQuery示例 XQuery FLWOR XQuery HTML XQuery条款 XQuery语法 XQuery添加 XQuery选择 XQuery函数

XML DTD

DTD简介 DTD构建基块 DTD元素 DTD属性 DTD元素 vs Attr DTD实体 DTD 示例

XSD Schema

XSD简介 XSD如何 XSD <schema> XSD元素 XSD属性 XSD限制

XSD Complex

XSD元素 XSD Empty XSD元素 Only 仅限XSD文字 XSD混合 XSD指标 XSD <any> XSD <anyAttribute> XSD替代 XSD示例

XSD Data

XSD字串 XSD日期 XSD数值 XSD杂项 XSD 参考

Web Services

XML服务 XML WSDL XML SOAP XML RDF XML RSS

参考

DOM节点类型 DOM节点 DOM节点List DOM NamedNodeMap DOM文档 DOM元素 DOM属性 DOM文字 DOM CDATA DOM评论 DOM XMLHttpRequest DOM解析器 XSLT元素 XSLT / XPath函数

XML DOM Node List


A list of nodes is returned by the getElementsByTagName() method and the childNodes property.


来试试吧 - 示例

The examples below use the XML file books.xml.

Get the text from the first <title> element
This example uses the getElementsByTagName() method to get the text from the first <title> element in "books.xml".

Loop through nodes using the length property
This example uses node list and the length property to loop through all <title> elements in "books.xml"

Get the attribute of an element
This example uses a attribute list to get attribute from the first <book> element in "books.xml".

×

Header


DOM节点列表

When using properties or methods like childNodes or getElementsByTagName(), a node list object is returned.

A node list object represents a list of nodes, in the same order as in the XML.

Nodes in the node list are accessed with index numbers starting from 0.

The following image represents a node list of the <title> elements in "books.xml":

DOM node list

Suppose "books.xml" is loaded into the variable xmlDoc.

This code fragment returns a node list of title elements in "books.xml":

x = xmlDoc.getElementsByTagName("title");

After the execution of the statement above, x is a node list object.

The following code fragment returns the text from the first <title> element in the node list (x):

Example

var txt = x[0].childNodes[0].nodeValue;
来试试吧 »

After the execution of the statement above, txt = "Everyday Italian".



Node List Length

A node list object keeps itself up-to-date. If an element is deleted or added, the list is automatically updated.

The length property of a node list is the number of nodes in the list.

This code fragment returns the number of <title> elements in "books.xml":

x = xmlDoc.getElementsByTagName('title').length;

After the execution of the statement above, the value of x will be 4.

The length of the node list can be used to loop through all the elements in the list.

This code fragment uses the length property to loop through the list of <title> elements:

Example

x = xmlDoc.getElementsByTagName('title');
xLen = x.length;

for (i = 0; i <xLen; i++) {
    txt += x[i].childNodes[0].nodeValue) + " ";
}

Output:

Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
来试试吧 »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Set the x variable to hold a node list of all title elements
  3. Collect the text node values from <title> elements

DOM属性 List (Named Node Map)

The attributes property of an element node returns a list of attribute nodes.

This is called a named node map, and is similar to a node list, except for some differences in methods and properties.

An attribute list keeps itself up-to-date. If an attribute is deleted or added, the list is automatically updated.

This code fragment returns a list of attribute nodes from the first <book> element in "books.xml":

x = xmlDoc.getElementsByTagName('book')[0].attributes;

After the execution of the code above, x.length = is the number of attributes and x.getNamedItem() can be used to return an attribute node.

This code fragment gets the value of the "category" attribute, and the number of attributes, of a book:

Example

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

txt = x.getNamedItem("category").nodeValue + " " + x.length;

Output:

cooking 1
来试试吧 »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Set the x variable to hold a list of all attributes of the first <book> element
  3. Get the value of the "category" attribute and the length of the attribute list




W3Schools is optimized for learning and training. 示例 might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2021 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.