html dom 替换节点,XML DOM替换节点

在本章中,我们将学习XML DOM对象中的替换节点操作。DOM中的所有内容都保存在分层信息单元中,替换节点提供了另一种更新这些指定节点或文本节点的方法。

以下是替换节点的两个方法 -

replaceChild()

replaceData()

1. replaceChild()方法

replaceChild()方法用新节点替换指定的节点。

语法

insertData()具有以下语法 -

Node replaceChild(Node newChild, Node oldChild) throws DOMException

其中,

newChild - 是放入子列表的新节点。

oldChild - 是列表中要替换的节点。

此方法返回已替换的节点。

示例

以下示例(replacenode.html)将XML文档(node.xml)解析为XML DOM对象,并使用新节点替换指定的节点。

function loadXMLDoc(filename) {

if (window.XMLHttpRequest) {

xhttp = new XMLHttpRequest();

} else { // code for IE5 and IE6

xhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xhttp.open("GET",filename,false);

xhttp.send();

return xhttp.responseXML;

}

xmlDoc = loadXMLDoc("/node.xml");

x = xmlDoc.documentElement;

z = xmlDoc.getElementsByTagName("FirstName");

document.write("Content of FirstName element before replace operation
");

for (i=0;i

document.write(z[i].childNodes[0].nodeValue);

document.write("
");

}

//create a Employee element, FirstName element and a text node

newNode = xmlDoc.createElement("Employee");

newTitle = xmlDoc.createElement("Name");

newText = xmlDoc.createTextNode("MS Dhoni");

//add the text node to the title node,

newTitle.appendChild(newText);

//add the title node to the book node

newNode.appendChild(newTitle);

y = xmlDoc.getElementsByTagName("Employee")[0]

//replace the first book node with the new node

x.replaceChild(newNode,y);

z = xmlDoc.getElementsByTagName("FirstName");

document.write("Content of FirstName element after replace operation
");

for (i = 0;i

document.write(z[i].childNodes[0].nodeValue);

document.write("
");

}

执行上面示例代码,得到以下结果 -

306f8295662f742c4991b105cb22956b.png

2. replaceData()方法

replaceData()方法用指定的字符串替换从指定的16位单偏移量开始的字符。

语法

replaceData()具有以下语法 -

void replaceData(int offset, int count, java.lang.String arg) throws DOMException

其中,

offset - 是开始替换的偏移量。

count - 要替换的16位单元的数量。 如果偏移量和计数之和超过长度,则替换数据末尾的所有16位单元。

arg - 必须替换的DOMString字符串范围。

示例

以下示例(replace_data.html)将XML文档(node.xml)解析为XML DOM对象并替换它。

function loadXMLDoc(filename) {

if (window.XMLHttpRequest) {

xhttp = new XMLHttpRequest();

} else{ // code for IE5 and IE6

xhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xhttp.open("GET",filename,false);

xhttp.send();

return xhttp.responseXML;

}

xmlDoc = loadXMLDoc("/node.xml");

x = xmlDoc.getElementsByTagName("ContactNo")[0].childNodes[0];

document.write("ContactNo before replace operation: "+x.nodeValue);

x.replaceData(1,2,"XX");

document.write("
");

document.write("ContactNo after replace operation: "+x.nodeValue);

在上面的例子中 -

x.replaceData(1,2,"XX"); - 这里最后一个参数保存指定元素的文本,其文本从位置1开始直到长度:2由新文本“XX”替换。

执行上面示例代码,得到以下结果 -

aaeb6727c54eff895b1b4e6b62a622e7.png

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值