节点属性:nodeName、nodeValue 以及 nodeType。
下面的例子使用 XML 文件 books.xml。
函数 loadXMLDoc(),位于外部 JavaScript 中,用于加载 XML 文件。
获取元素节点的节点名称
本例使用 nodeName 属性来获取 “books.xml” 中根元素的节点名称。
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); document.write(xmlDoc.documentElement.nodeName); </script> </body> </html>
从文本节点获取文本
本例使用 nodeValue 属性来获取 “books.xml” 中第一个 <title> 元素的文本。
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
更改文本节点中的文本
本例使用 nodeValue 属性来更改 “books.xml” 中第一个 <title> 元素的文本。
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
获取元素节点的节点名称和类型
本例使用 nodeName 和 nodeType 属性来获取 “books.xml” 中根元素的节点名称和类型。
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); document.write(xmlDoc.documentElement.nodeName); document.write("<br />"); document.write(xmlDoc.documentElement.nodeType); </script> </body> </html>
在 XML 文档对象模型 (DOM) 中,每个节点都是一个对象。
对象拥有方法(功能)和属性(关于对象的信息),并可通过 JavaScript 进行访问和操作。
三个重要的 XML DOM 节点属性是:
nodeName 属性规定节点的名称。
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); document.write(xmlDoc.documentElement.nodeName); </script> </body> </html>
nodeValue 属性规定节点的值。
下面的代码检索第一个 <title> 元素的文本节点的值:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue;
结果:txt = “Harry Potter”
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
下面的代码更改第一个 <title> 元素的文本节点的值:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking";
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
nodeType 属性规定节点的类型。
nodeType 是只读的。
元素类型 | 节点类型 |
---|---|
元素 | 1 |
属性 | 2 |
文本 | 3 |
注释 | 8 |
文档 | 9 |
<html> <head> <script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("/example/xdom/books.xml"); document.write(xmlDoc.documentElement.nodeName); document.write("<br />"); document.write(xmlDoc.documentElement.nodeType); </script> </body> </html>