XML DOM Node List
- Kwanan Bayanin kwayar saurayi DOM
- Sakamako DOM yanar da kwayar saurayi
节点列表由 getElementsByTagName() 方法和 childNodes 属性返回。
实例
下面的例子使用 XML 文件 books.xml。
函数 loadXMLDoc(),位于外部 JavaScript 中,用于加载 XML 文件。
- 从第一个
元素获取文本 - 本例使用 getElementsByTagName() 方法从 "books.xml" 中的第一个
元素获取文本。 - 通过使用 length 属性来循环节点
- 本例使用节点列表和 length 属性来循环 "books.xml" 中的所有
元素。 - 获取元素的属性
- 本例使用属性列表从 "books.xml" 中的第一个
元素获取属性。
DOM Node List
当使用诸如 childNodes 或 getElementsByTagName() 属性或方法时,会返回 NodeList 对象。
NodeList 对象表示节点的列表,以 XML 中的相同顺序。
使用从 0 开始的下标来访问节点列表中的节点。
The following image represents "books.xml" A node list of <title> elements:

Bai ce: na kwayar saurayin da a samar da amfani da loadXMLDoc() Load "books.xml" into xmlDoc and return "books.xml" A node list of a title element:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title");
After the above statement is executed, x becomes a NodeList object.
The following code snippet returns the text from the first <title> element in the node list x:
txt=x[0].childNodes[0].nodeValue;
After the above statement is executed, txt = "Everyday Italian".
Node List Length
The NodeList object will keep its update. If elements are deleted or added, the list will update automatically.
The length property of the node list is the number of nodes in the list.
Bai ce: na kwayar saurayin da a samar da amfani da loadXMLDoc() Gudanar "books.xml" Load xmlDoc, and return the number of <title> elements in "books.xml":
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('title').length;
After the above statement is executed, x = 4.
The length of the node list can be used to loop through all elements in the list.
The following code snippet uses the length property to traverse the list of <title> elements:
xmlDoc=loadXMLDoc("books.xml"); //the x variable will hold a node list x=xmlDoc.getElementsByTagName('title'); for (i=0;i<x.length;i++) { document.write(x[i].childNodes[0].nodeValue); document.write("<br />"); }
Bai ciki:
Harry Potter Everyday Italian XQuery Kick Start Learning XML
Bai ce:
- Gudanar amfani da loadXMLDoc() Gudanar "books.xml" Load xmlDoc
- Set the x variable to save the node list of all title elements
- Output the value from the text node of all <title> elements
DOM Attribute List (Named Node Map)
The attributes property of the element node returns a list of attribute nodes.
This is called Named Node Map, it is similar to the node list except for some differences in methods and properties.
List of attributes will keep its update. If attributes are deleted or added, the list will update automatically.
Bai ce: na kwayar saurayin da a samar da amfani da loadXMLDoc() Gudanar "books.xml" da xmlDoc, da kuma samar da saurayin daga "books.xmlBai ciki: na farko <book> da a cikin "
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book')[0].attributes;
Kwakwas na baya na wannan code, x.length yana da kwanan da a cikin saurayin, amfani da x.getNamedItem() don samar da saurayin.
Bai ce: na kwayar saurayin "category" na book, da kuma kwanan da a cikin saurayin
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0].attributes; document.write(x.getNamedItem("category").nodeValue); document.write("<br />" + x.length);
Bai ciki:
children 1
Bai ce:
- Gudanar amfani da loadXMLDoc() Gudanar "books.xml" da a cikin xmlDoc
- Gudanar x da yana da kwanan aiki na wucin gudanar daga <book> saurayin farko
- Gudanar da kwanan da a baya "category"
- Gudanar da sakamako na kwanan aiki
- Kwanan Bayanin kwayar saurayi DOM
- Sakamako DOM yanar da kwayar saurayi