当前位置: 首页 > 网络学院 > 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   浏览: 904 ::
收藏到网摘: n/a

Examples
案例

In the examples below, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
在下述案例中,我们需要用到下面这两个XML文件:books.xml和Java脚本函数 loadXMLDoc()

替换nodelist[节点列表]中的一个节点
This example uses replaceChild() to replace the last child in a node list.
这个案例将使用replaceChild()方法来替换一个节点列表中的最后一个子节点。

替换一个文本节点中的数据
This example uses replaceData() to replace data in a text node.
这个案例将使用replaceData()方法来替换一个文本节点中的数据。


Replace a Node in a Node List
替换一个节点列表中的一个节点

The replaceChild() method is used to replace a node in a node list.
replaceChild()方法的作用是:替换一个节点列表中的一个节点。

The following code fragment creates a new <book> element that will replace the last <book> element:
下述代码片断将创建一个全新的<book>元素来替代最后一个<book>元素:

//check if last child node is an element node
function get_lastchild(n)
{
var x=n.lastChild;
while (x.nodeType!=1)
{
x=x.previousSibling;
}
return x;
}
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.documentElement;
//create a book element, title element and a text node
var newNode=xmlDoc.createElement("book");
var newTitle=xmlDoc.createElement("title");
var newText=xmlDoc.createTextNode("A Notebook");
//add the text node to the title node,
//and add the title node to the book node
newTitle.appendChild(newText);
newNode.appendChild(newTitle);
//replace the last node with the new node
x.replaceChild(newNode,get_lastchild(x));

Note: Internet Explorer will skip white-space text nodes that are generated between nodes (e.g. new-line characters), while Mozilla will not. So, in the example above, the get_lastchild() function checks the node type of the last child node of the parameter.
注意:IE浏览器将跳过在节点中产生的空格文本节点(如:跳过新一行字符);然而Mozilla浏览器却不会。因此,在上述案例中,需要使用get_lastchild()函数检验参数中最后一个子节点的节点类型。

Element nodes has a nodeType of 1, so if not the last child of the node in the parameter is an element node, it moves to the previous node, and checks if this node is an element node. This continues until the last child node (which must be an element node) is found. This way, the result will be correct in both Internet Explorer and Mozilla.
元素节点的节点类型为1,因此,如果参数中的最后一个子节点不是一个元素节点,那么它将继续向上一个节点移动,检查这个节点是不是一个元素节点,直到第一次发现这个元素节点为止。通过使用这个方法,可以同时在IE浏览器和Mozilla浏览器中显示正确的结果。


Replace Data In a Text Node
替换一个文本节点中的数据

The replaceData() method is used to replace data in a text node.
replaceData()方法的作用是:替换一个文本节点中的数据。

The replaceData() method has three parameters:
replaceData()方法包含下面三个参数:

  • offset - Where to begin replacing characters. Offset value starts at zero
    offset - 指定替换字符的起始位置。起始值为0
  • length - How many characters to replace
    length - 指定需要替换的全部字符数量
  • string - The string to insert
    string - 指定需要插入的字符

The following code fragment will replace the eight first characters from the text node in the first <title> element with "Easy":
下述代码片断将用“Easy”来替换位于第一个<title>元素中文本节点的8个首字符:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.replaceData(0,8,"Easy");

评论 (0) All

登陆 | 还没注册?