XML DOM节点操作:改变节点

nodeValue 属性用于改变节点值。

setAttribute() 方法用于改变属性的值。

实例

下面的例子使用 XML 文件 books.xml

函数 loadXMLDoc(),位于外部 JavaScript 中,用于加载 XML 文件。

改变元素的文本节点

本例使用 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="Hello World";

document.write(x.nodeValue);
</script>
</body>
</html>

通过使用 setAttribute 来改变属性的值

本例使用 setAttribute() 方法来改变第一个 <book> 的 “category” 属性的值。

<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('book');

x[0].setAttribute("category","child");

document.write(x[0].getAttribute("category"));
</script>
</body>
</html>

通过使用 nodeValue 来改变属性值

本例使用 nodeValue 属性来改变第一个 <book> 的 “category” 属性的值。

<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("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="child";

document.write(y.nodeValue);
</script>
</body>
</html>

改变元素的值

在 DOM 中,每种成分都是节点。元素节点没有文本值。

元素节点的文本存储在子节点中。该节点称为文本节点。

改变元素文本的方法,就是改变这个子节点(文本节点)的值。

改变文本节点的值

nodeValue 属性可用于改变文本节点的值。

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

xmlDoc=loadXMLDoc("books.xml");

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

例子解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 获取第一个 <title> 元素的文本节点
  • 把此文本节点的节点值更改为 “Hello World”
<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="Hello World";

document.write(x.nodeValue);
</script>
</body>
</html>

遍历并更改所有 <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");

for (i=0;i<x.length;i++)
{
x[i].childNodes[0].nodeValue="Unavaliable";
document.write(x[i].childNodes[0].nodeValue);
document.write("<br />");
}
</script>
</body>
</html>

改变属性的值

在 DOM 中,属性也是节点。与元素节点不同,属性节点拥有文本值。

改变属性的值的方法,就是改变它的文本值。

可以通过使用 setAttribute() 方法或属性节点的 nodeValue 属性来完成这个任务。

通过使用 setAttribute() 来改变属性

setAttribute() 方法设置已有属性的值,或创建新属性。

下面的代码改变 <book> 元素的 category 属性:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","child");

例子解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 获取第一个 <book> 元素
  • 把 “category” 属性的值更改为 “child”
<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('book');

x[0].setAttribute("category","child");

document.write(x[0].getAttribute("category"));
</script>
</body>
</html>

遍历所有 <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');

//add a new attribute to each title element
for(i=0;i<x.length;i++)
{
x[i].setAttribute("edition","first");
}

//Output title and edition value
for (i=0;i<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(x[i].getAttribute('edition'));
document.write("<br />");
}
</script>
</body>
</html>

注释:如果属性节点不存在,则创建一个新属性(拥有指定的名称和值)。

通过使用 nodeValue 改变属性

nodeValue 属性可用于更改属性节点的值:

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="child";

例子解释:

  • 通过使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
  • 获取第一个 <book> 元素的 “category” 属性
  • 把该属性节点的值更改为 “child”
<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("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="child";

document.write(y.nodeValue);
</script>
</body>
</html>

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

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

相关推荐

发表回复

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