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 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

你可能感兴趣的:(VB,Script)