`
hipeace87
  • 浏览: 170514 次
  • 性别: Icon_minigender_1
  • 来自: 山东菏泽
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics