配置XML Diff VBscript以传递多个XML

时间:2017-07-24 10:47:37

标签: xml vbscript diff

以下脚本是为了比较两个XML文件而编写的,它被设计为标记任何差异,而不管元素的位置如何。

如何重新配置​​此工具以在单个文件位置中引入和比较多个XML并与一个XML进行比较?

Dim o_xmlDocObj1 : Set o_xmlDocObj1 = CreateObject("Microsoft.XMLDOM")
Dim o_xmlDocObj2 : Set o_xmlDocObj2 = CreateObject("Microsoft.XMLDOM")
Dim o_objFSO : Set o_objFSO = CreateObject("Scripting.FileSystemObject")
Dim o_objShell : Set o_objShell = CreateObject("WScript.Shell")
Dim o_Nodes1, o_Nodes2
Dim o_ChildNode1, o_ChildNode2
Dim w_Found 
Dim w_ReturnString1 : w_ReturnString1 = ""
Dim w_ReturnString2 : w_ReturnString2 = ""
Dim o_objFile

Const c_ForWriting = 2, c_ForReading = 1, c_ForAppending = 8
Const c_TristateUseDefault = -2, c = -1, c_TristateFalse = 0

P_File1 = InputBox("Compare File:")
P_File2 = InputBox("Compare to File:")
P_resultFile = InputBox("Return Result to:")

If Not o_objFSO.FileExists(P_File1) Then
    MsgBox "File does not exist" & P_File1
    WScript.Quit 1
End If

If Not o_objFSO.FileExists(P_File2) Then
    MsgBox "File does not exist" & P_File2
    WScript.Quit 1
End If

If o_xmlDocObj1.load(P_File1) And o_xmlDocObj2.load(P_File2) Then
    Set o_Nodes1 = o_xmlDocObj1.documentElement
    Set o_Nodes2 = o_xmlDocObj2.documentElement

    If Not (o_Nodes1 Is Nothing and o_Nodes2 Is Nothing) Then
        For Each o_ChildNode1 In o_Nodes1.childNodes
            w_Found = False
            For Each o_ChildNode2 In o_Nodes2.childNodes
                If o_ChildNode1.xml = o_ChildNode2.xml Then
                    w_Found = True
                    Exit For
                Else
                    ' Check Attribute level if Ref and PAthis the same
                    If (o_ChildNode1.getAttribute("Ref") = o_ChildNode2.getAttribute("Ref")) Then 
                        If (o_ChildNode1.getAttribute("PathAll") = o_ChildNode2.getAttribute("PathAll")) Then 
                            w_Found = True
                            For x = 0 To (o_ChildNode2.Attributes.length - 1)
                                sAttrName = o_ChildNode2.Attributes.Item(x).nodeName
                                 avalue=o_ChildNode2.Attributes.Item(x).nodeValue
                                 If (o_ChildNode1.getAttribute(sAttrName) <> avalue) then
                                     w_Found = False
                                    Exit For
                                 End If
                             Next
                         End If
                     End If
                End If
            Next

            If w_Found = False Then 
                w_ReturnString1 = w_ReturnString1 &  o_ChildNode1.xml & vbCrLf
            End If
        Next

        For Each o_ChildNode2 In o_Nodes2.childNodes
            w_Found = False
            For Each o_ChildNode1 In o_Nodes1.childNodes
                 If o_ChildNode1.xml = o_ChildNode2.xml Then
                    w_Found = True
                    Exit For
                Else
                    ' Check Attribute level if Ref and PAthis the same
                    If (o_ChildNode2.getAttribute("Ref") = o_ChildNode1.getAttribute("Ref")) Then 
                        If (o_ChildNode2.getAttribute("PathAll") = o_ChildNode1.getAttribute("PathAll")) Then 
                            w_Found = True
                            For x = 0 To (o_ChildNode1.Attributes.length - 1)
                                sAttrName = o_ChildNode1.Attributes.Item(x).nodeName
                                 avalue=o_ChildNode1.Attributes.Item(x).nodeValue
                                 If (o_ChildNode2.getAttribute(sAttrName) <> avalue) then
                                     w_Found = False
                                    Exit For
                                 End If
                             Next
                         End If
                     End If
                End If
            Next

            If w_Found = False Then 
                w_ReturnString2 = w_ReturnString2 &  o_ChildNode2.xml & vbCrLf
            End If
        Next
    End If

    If P_resultFile <> "" Then
        Call o_objFSO.CreateTextFile(P_resultFile,True)
        Set o_objFile = o_objFSO.OpenTextFile(P_resultFile,c_ForAppending)
        Call o_objFile.WriteLine(Now)
        Call o_objFile.WriteLine("Compare File " & P_File1 & " To " & P_file2)
        Call o_objFile.WriteLine("-----------------------------------------")
        Call o_objFile.WriteLine("Scheme not exist in  " & P_File2)
        If w_ReturnString1 = "" Then 
            Call o_objFile.WriteLine("*NONE")
        Else
            Call o_objFile.WriteLine(w_ReturnString1)
        End If 
        Call o_objFile.WriteLine("Scheme not exist in  " & P_File1)
        If w_ReturnString2 = "" Then 
            Call o_objFile.WriteLine("*NONE")
        Else
            Call o_objFile.WriteLine(w_ReturnString2)
        End If

        Call o_objFile.WriteLine(Now)
        o_objShell.Run("notepad " & P_resultFile)
    Else
        If w_ReturnString1 <> "" Then
            Call MsgBox(w_ReturnString1,vbOK,"Schemes not exist in " & P_File2)
        End If

        If w_ReturnString2 <> "" Then
            Call MsgBox(w_ReturnString1,vbOK,"Schemes not exist in " & P_File2)
        End If

        If w_ReturnString1 = "" And w_ReturnString2 = "" Then
            Call MsgBox("Both files are the same")
        End If
    End If
    WScript.Quit 0
Else
    MsgBox "Fail to load XML file" 
    WScript.Quit 1
End If 

0 个答案:

没有答案