VBS操作XML文档,拷贝结点

Dim xmlDoc,xmlRoot,xmlNode,lastNode,newNode
Dim doc,docRoot,i,flag
Dim strNodeName,strPath,docPath
Dim xmlFolder,docFolder,oshell
Set oshell = CreateObject("Shell.Application")
Set xmlFolder = oshell.BrowseForFolder(0, "选择目标XML文档所在的目录", 0, ssfDRIVES)
  strPath = xmlFolder.Self.Path & "\jz-mapping.xml"
Set docFolder = oshell.BrowseForFolder(0,"请选择源XML文档所在的目录",0,ssfDRIVES)
  docPath = docFolder.Self.Path & "\jz-mapping.xml"
strNodeName = InputBox ("请输入要拷贝的结点ID值","输入ID值")

flag = True
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 = False
  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
MsgBox  "已存在要拷贝的结点!",,"Bug提示"
End If

上次发的那个要在CMD下运行,还要传参数
这个在运行时会弹出相应的对话框
方便了操作

(原 http://hipeace87.iteye.com/blog/290520

你可能感兴趣的:(xml)