当前位置: 首页 > 网络学院 > 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   浏览: 889 ::
收藏到网摘: 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 createElement() to create a new element node, and appendChild() to add it to a node list.
此例通过createElement() 方法创建了一个新的元素,并将其放入节点列表。

创建一个新的属性
This example uses createAttribute() to create a new attribute node, and setAttributeNode() to insert it to an element.
此例通过createAttribute() 方法创建了一个新的属性,并把它放入了现有的元素中。

创建了一个新的文本节点
创建了一个新的文本节点
This example uses createTextNode() to create a new text node, and appendChild() to add it to a node list.
此例通过createTextNode() method创建了一个新的文本节点,并把它放入了现有的元素中。

创建了一个新的CDATA片段
This example uses createCDATAsection() to create a CDATA section node, and appendChild() to add it to a node list.
此例通过createCDATAsection() method创建了一个CDATA片断,并把它放入节点列表中。

创建一个注释节点
This example uses createComment() to create a comment node, and appendChild() to add it to a node list.
这个案例使用createComment()创建了一个注释节点,并通过使用appendChild()将它添加到了节点列表中。

 


 

Create an Element
创建一个元素

The createElement() method creates a new element node.
createElement()方法创建了一个全新的元素节点。

The following code fragment creates an element (<edition>), and adds it after the last child of each <book> element:
下述代码片断创建了一个全新<edition>元素,并把它添加到所有的<book>元素中:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition');
x[i].appendChild(newel);
}

 


Create an Attribute
创建一个属性

The createAttribute() creates a new attribute node.
createAttribute()创建了一个全新的attribute[属性]节点。

The following code fragment creates an "edition" attribute and adds it to all <book> elements:
下述代码片断创建了一个全新的"edition"属性,并把它添加到了<book>所有的属性中:

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

 


Create a Text Node
创建一个文本节点

The createTextNode() method creates a new text node.
TextNode()创建了一个全新的文本节点。

The following code fragment creates an element (<edition>), with a text node ('First') in it, and adds it after the last child of each <book> element:
下述代码片断创建了一份全新的带有文本节点的<edition>元素,并把它添加到所有的<book>元素中:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel,newtext
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement('edition');
newtext=xmlDoc.createTextNode('First');
newel.appendChild(newtext);
x[i].appendChild(newel);
}

 


Create a CDATA Section Node
创建一个CDATA片断

The createCDATASection() method creates a new CDATA section node.
createCDATASection()方法创建了一个全新的CDATA片断节点。

The following code fragment creates a CDATA section, and adds it after the last child of each <book> element:
下述代码片断展示了如何创建一个CDATA片断,并指定了如何将它添加到每一个<book>元素中 :

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newCDATA,newtext;
newtext="Special Offer & Book Sale";
for (i=0;i<x.length;i++)
{
newCDATA=xmlDoc.createCDATASection(newtext);
x[i].appendChild(newCDATA);
}

 


Create a Comment Node
创建一个注释节点

The createComment() method creates a new comment node.
使用createComment()方法创建一个全新的注释节点。

The following code fragment creates a comment node, and adds it after the last child of each <book> element:
下述代码片段创建了一个注释节点,并将其添加在每个<book>元素中最后一个子元素的后面:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newComment,newtext;
newtext="Revised September 2006";
for (i=0;i<x.length;i++)
{
newComment=xmlDoc.createComment(newtext);
x[i].appendChild(newComment);
}

评论 (0) All

登陆 | 还没注册?