VBS操作XML文档,拷贝结点

'执行方式 wscritp.exe 目标文件路径,要拷贝的结点的ID属性值,源文件路径
Dim xmlDoc,xmlRoot,xmlNode,lastNode,newNode
Dim doc,docRoot,i,flag
Dim strNodeName,strPath,docPath
strPath = Wscript.Arguments(0) '目标文件
strNodeName = Wscript.Arguments(1) '源文件中结点
docPath = Wscript.Arguments(2) '源文件名称
flag = False
Rem 加载目标文件
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.load strPath
If xmlDoc.parseError.errorCode <> 0 Then
Wscript.Echo "错误:" & Chr(13) & xmlDoc.parseError.reason
End If
Set xmlRoot = xmlDoc.documentElement
i = 0
Do While i<xmlRoot.childNodes.length
Set newNode = xmlRoot.childNodes.item(i)
If newNode.getAttribute("id") = strNodeName Then
flag = True
Exit Do
Else
i = i + 1
End If
loop

If flag Then
Rem 加载源文件,查找要拷贝的结点
Set doc =CreateObject("Microsoft.XMLDOM")
doc.async = False
doc.load docPath
If doc.parseError.errorCode <> 0 Then
Wscript.Echo "错误:" & Chr(13) & doc.parseError.reason
End If
Set docRoot = doc.documentElement
i = 0
Do While i < docRoot.childNodes.length
Set newNode = docRoot.childNodes.item(i)
If newNode.getAttribute("id") = strNodeName Then
Exit Do
Else
i = i + 1
End If
loop

Set lastNode = xmlRoot.lastChild
xmlDoc.documentElement.insertBefore newNode,lastNode
xmlDoc.Save strPath
Else
Wscript.Echo "已存在要拷贝的结点!"
End If
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值