XML DOM:节点信息

节点属性: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
  • nodeValue
  • nodeType

nodeName 属性

nodeName 属性规定节点的名称。

  • nodeName 是只读的
  • 元素节点的 nodeName 与标签名相同
  • 属性节点的 nodeName 是属性的名称
  • 文本节点的 nodeName 永远是 #text
  • 文档节点的 nodeName 永远是 #document
<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 属性

nodeValue 属性规定节点的值。

  • 元素节点的 nodeValue 是 undefined
  • 文本节点的 nodeValue 是文本自身
  • 属性节点的 nodeValue 是属性的值

例子 1:获取元素的值

下面的代码检索第一个 <title> 元素的文本节点的值:

xmlDoc=loadXMLDoc("books.xml");

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

结果:txt = “Harry Potter”

代码解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 获取第一个 <title> 元素节点的文本节点
  • 把 txt 变量设置为文本节点的值
<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>

例子 2:更改元素的值

下面的代码更改第一个 <title> 元素的文本节点的值:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";

代码解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 获取第一个 <title> 元素节点的文本节点
  • 把文本节点的值更改为 “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 属性规定节点的类型。

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>

作者:terry,如若转载,请注明出处:https://www.web176.com/xmldom/10851.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年2月28日
下一篇 2023年2月28日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注