当前位置: 首页 > 网络学院 > 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   浏览: 1045 ::
收藏到网摘: 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()

删除一个元素
This example uses removeChild() to remove the last <book> element from the loaded XML.
该案例通过removeChild() 方法从已加载的XML文件中删除了未尾的<book>元素。

从一个文本节点中删除文本
This example uses deleteData() to remove text from a text node in the loaded XML.
该案例通过deleteData() 方法从已加载的XML文件元素中删除删除了一个文本节点中的文本。

删除一个属性
This example uses removeAttribute() to remove all "category" attributes from the loaded XML.
该案例通过removeAttribute()方法从已加载的XML文件中删除了所有的"category"属性。

使用 removeAttributeNode()
This example uses removeAttributeNode() to remove all "category" attributes from the loaded XML.
该案例通过removeAttributeNode()方法从已加载的XML文件中删除所有"category"属性。


Remove an Element
删除一个元素

The removeChild() method can be used to remove a specified node.
removeChild()方法的作用是删除一个指定的节点。

The following code fragment will remove the last <book> element from the loaded xml:
下述代码片断会从已加载的XML文件中删除未尾的<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;
x.removeChild(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浏览器中显示正确的结果。


Remove Text From an Element
从一个元素中删除文本

The deleteData() method is used to remove data from a text node.
deleteData()的作用是从文本节点中删除指定范围的数据。

The deleteData() method has two parameters:
deleteData()包含2个参数:

  • offset - Where to begin removing characters. Offset value starts at zero
    起始端(offset)- 删除字符的起始位置。起始端的值从0开始。
  • count - How many characters to delete
    计数(count)- 指定需要删除的字符数量

The following code fragment will remove the first nine characters from the first <title> element in the loaded XML:
接下来的代码片断会从已加载的XML文件的第一个<title>元素中删除第一行字符:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.deleteData(0,9);

 


Remove an Attribute
删除一个属性

The removeAttribute() method is used to remove an attribute node.
removeAttribute()的作用是删除一个属性节点。

The following code fragment will remove all "category" attributes in each <book> element:
下述代码片段将从已加载的XML文件中删除所有的"category"属性:

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

 


removeAttributeNode()

The removeAttributeNode() method is used to remove an attribute node.
removeAttributeNode()方法的作用是删除一个属性节点。

The following code fragment will remove all "category" attributes in each <book> element:
下述代码片断将从已加载的XML文件中删除所有"category"属性:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
for(i=0;i<x.length;i++)
{
attnode=x.item(i).getAttributeNode("category")
old_att=x.item(i).removeAttributeNode(attnode);
document.write("Removed attribute: " + old_att.name + "<br />");
}

Output:
输出结果:

Removed attribute: category
Removed attribute: category
Removed attribute: category
Removed attribute: category

评论 (0) All

登陆 | 还没注册?