Categories: XML DOM教程

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

这个人很懒,什么都没有留下~

Share
Published by
terry

Recent Posts

vue:页面注入js修改input值

一般会直接这样写: let z…

8 小时 ago

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago