当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM 设置节点

XML DOM
DOM Attribute
DOM Text
DOM CDATA
DOM Comment
DOM HttpRequest
DOM ParseError
DOM 校验器
DOM 介绍
DOM 摘要
DOM 案例
DOM 节点树
DOM 访问节点树
DOM 节点信息
DOM 文档执行
DOM 节点导航

XML DOM 中的 DOM 设置节点


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 921 ::
收藏到网摘: n/a

Examples
案例

In the examples below, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
在下述案例中,我们将使用这个XML文件:books.xml,以及JavaScript 函数“loadXMLDoc()”。

设置一个新的属性和属性值
This example uses setAttribute() to set a new attribute/attribute value.
该案例通过setAttribute() 方法设置了一个新属性 / 属性值。

创建一个新属性节点
This example uses createAttribute() to create a new attribute node, and setAttributeNode() to insert it to an element.
该案例通过createAttribute() 方法创建了一个新属性节点,并且使用setAttributeNode()将该属性节点插入到一个元素中。

改变属性值
This example uses the setAttribute() method to change the value of an existing attribute.
该案例通过setAttribute()方法更改现有属性值。

改变一个项目的值
This example uses the getNamedItem() method to change the value of an existing attribute.
该案例通过getNamedItem() 方法更改现有属性值。


Set a New Attribute and Attribute Value
设置一个新属性和属性值

The setAttribute() method can be used to change the value of an existing attribute, or to create a new attribute/attribute value for an element.
setAttribute()的作用:改变元素现有的属性值,或创建一个新的属性/属性值。

The following code fragment adds a new attribute/attribute value to each <book> element:
下述代码片断在每个<book>元素中都添加了一个全新的属性/属性值:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
{
x.item(i).setAttribute("edition","first");
}

 


Another Way to Create a New Attribute
创建新属性的其它方法

The createAttribute() is used to create a new attribute node.
createAttribute() 的作用:创建一个全新的属性节点。

The following code fragment uses createAttribute() to create a new attribute node, and setAttributeNode() to insert it to an element.:
下述代码片断使用createAttribute()方法创建了一个全新的属性节点,并通过setAttributeNode()方法将它插入到一个元素中:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newatt;
for (i=0;i<x.length;i++)
{
newatt=xmlDoc.createAttribute("edition").value="first";
x[i].setAttributeNode(newatt);
}

 


Change an Attribute Value
更改一个属性值

The setAttribute() method can be used to change the value of an existing attribute, or to create a new attribute/attribute value for an element.
setAttribute() method作用:改变元素现有的属性值,或创建一个新的属性/属性值。

The following code fragment changes the value of the existing "category" attribute (in each <book> element):
下述代码片断将每个<book>元素中现有的"category"属性值设置成新值"BESTSELLER":

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
{
x.item(i).setAttribute("category","bestseller");
}

 


Change an Item's Value
更改一个项的值

The getNamedItem() method can be used to change the value of an existing item.
getNamedItem() method作用是更改现有项的值。

The following code fragment also changes the value of the existing "category" attribute (in each <book> element):
下述代码片断将每个<book>元素中的"category" 属性值更改为"BESTSELLER":

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++)
{
var att=x.item(i).attributes.getNamedItem("category");
att.value="bestseller";
}

评论 (0) All

登陆 | 还没注册?