-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_and_compare_multiple_XMLs.vba
257 lines (214 loc) · 8.28 KB
/
search_and_compare_multiple_XMLs.vba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'
' MIT License
'
' Copyright (c) 2018 Praveen Lobo (praveenlobo.com)
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all
' copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'
'
' Refreshes the key list and searches for those keys in the file using the FileSearch power query
'
Sub RefreshFileSearch()
ClearPreviousResults
ThisWorkbook.Connections("Query - FileSearch").Refresh
End Sub
'
' Refreshes the key list and searches for those keys in the databse using the DBSearch power query
'
Sub RefreshDBSearch()
ClearPreviousResults
ThisWorkbook.Connections("Query - DBSearch").Refresh
End Sub
'
' Returns the range of previous compare results
'
Function CompareResultRange() As Range
On Error Resume Next
ThisWorkbook.Sheets("Main").ShowAllData
On Error GoTo 0
ThisWorkbook.Sheets("Main").Range("C2").Select
' Select all down only if there is any value
If IsEmpty(Range("C2").Offset(1, 0).Value) = False Then
Range(Selection, Selection.End(xlDown)).Select
End If
' Extend the selection to add six columns
Selection.Resize(Selection.Rows.count, 6).Select
Set CompareResultRange = Selection
End Function
'
' Clears the previous results
'
Sub ClearPreviousResults()
With CompareResultRange()
.ClearContents
.FormatConditions.Delete
.Interior.Color = xlNone
With .Borders
.LineStyle = xlNone
End With
End With
ThisWorkbook.Sheets("Main").Range("C2").Select
ActiveWindow.ScrollRow = 1
End Sub
'
' returns the first xml that matches a given key on a given sheet
' Note: assumes column A has the xmls starting at row 2
'
Function GetFileXML(SheetName As String, Key As String) As String
Dim xml As Range
Set xml = ThisWorkbook.Sheets(SheetName).Range("A2")
Do Until IsEmpty(xml)
If InStr(1, xml.Text, Key, vbTextCompare) > 0 Then
GetFileXML = xml.Text
Exit Function
End If
Set xml = xml.Offset(1, 0)
Loop
End Function
'
' returns the first xml from a row that matches a given key on a given sheet.
' Note: assumes that column A has the keys and columb B has the xmls starting row 2
'
Function GetDBXML(SheetName As String, Key As String) As String
Dim xml As Range
Set xml = ThisWorkbook.Sheets(SheetName).Range("B2")
Do Until IsEmpty(xml)
If InStr(1, xml.Offset(0, -1).Text, Key, vbTextCompare) > 0 Then
GetDBXML = xml.Text
Exit Function
End If
Set xml = xml.Offset(1, 0)
Loop
End Function
'
' Compares the two XMLs and displays mismatches
'
Sub CompareXMLs()
ClearPreviousResults
Dim CompareResultCell As Range
Set CompareResultCell = ThisWorkbook.Sheets("Main").Range("C2")
Dim xml As MSXML2.DOMDocument
Set xml = New MSXML2.DOMDocument
xml.async = False: xml.ValidateOnParse = False
Dim SourceTagTagDict As Object
Set SourceTagTagDict = CreateObject("Scripting.Dictionary")
SourceTagTagDict.CompareMode = vbBinaryCompare
Dim xmlString As String
Dim XmlNodes As IXMLDOMNodeList
Dim XmlNode As IXMLDOMNode
Dim Key As Range
Set Key = ThisWorkbook.Sheets("Main").Range("A2")
Dim count As Integer: count = 0
Dim CompareResult As String
Dim MissingSource As Boolean: MissingSource = False
Dim MissingTarget As Boolean: MissingTarget = False
' loop through each key and compare
Do Until IsEmpty(Key)
MissingSource = False
MissingTarget = False
SourceTagTagDict.RemoveAll
' Get the file xml string and cleanup
xmlString = GetFileXML("file", Key.Text)
If Not xml.LoadXML(xmlString) Then
MissingSource = True
GoTo SkipSource
End If
Set XmlNodes = xml.ChildNodes().Item(0).ChildNodes() 'Because We have only one XML document
' load the dictionary with tag and value
For Each XmlNode In XmlNodes
SourceTagTagDict.Add XmlNode.BaseName, XmlNode.Text
Next XmlNode
SkipSource:
' Get the DB xml string and clean up
xmlString = GetDBXML("database", Key.Text)
If Not xml.LoadXML(xmlString) Then
MissingTarget = True
GoTo SkipTarget
End If
Set XmlNodes = xml.ChildNodes().Item(0).ChildNodes() 'We have only one XML document
SkipTarget:
If MissingSource Or MissingTarget Then
CompareResultCell.Value = Key.Text
CompareResultCell.Offset(0, 1).Value = "NA"
CompareResultCell.Offset(0, 2).Value = "NA"
CompareResultCell.Offset(0, 3).Value = "NA"
CompareResultCell.Offset(0, 4).Value = "Missing " & IIf(MissingSource, "File ", "") & IIf(MissingTarget, "DB ", "") & "XML"
Set CompareResultCell = CompareResultCell.Offset(1, 0)
count = count + 1
GoTo NextKeyCell
End If
' we have xmls from file and db both. compare.
For Each XmlNode In XmlNodes
If SourceTagTagDict.Exists(XmlNode.BaseName) Then
' File xml matching tag exists
If StrComp(XmlNode.Text, SourceTagTagDict.Item(XmlNode.BaseName), vbBinaryCompare) <> 0 Then
' the value don't match
CompareResult = "Mismatch"
count = count + 1
Else
' the values match
CompareResult = "Match"
End If
' load the result
CompareResultCell.Value = Key.Text
CompareResultCell.Offset(0, 1).Value = XmlNode.BaseName
CompareResultCell.Offset(0, 2).Value = SourceTagTagDict.Item(XmlNode.BaseName)
CompareResultCell.Offset(0, 3).Value = XmlNode.Text
CompareResultCell.Offset(0, 4).Value = CompareResult
' remove the key from dictionary
SourceTagTagDict.Remove (XmlNode.BaseName)
Else
' No File XML matching tag; this is only in DB
CompareResultCell.Value = Key.Text
CompareResultCell.Offset(0, 1).Value = XmlNode.BaseName
CompareResultCell.Offset(0, 3).Value = XmlNode.Text
CompareResultCell.Offset(0, 4).Value = "DB Tag Only"
count = count + 1
End If
' move the cell one down
Set CompareResultCell = CompareResultCell.Offset(1, 0)
Next XmlNode
' Loop through File XML tag dictionary and print. Theese are in File xml only
Dim FileOnlyTag As Variant
For Each FileOnlyTag In SourceTagTagDict.Keys()
CompareResultCell.Value = Key.Text
CompareResultCell.Offset(0, 1).Value = FileOnlyTag
CompareResultCell.Offset(0, 2).Value = SourceTagTagDict.Item(FileOnlyTag)
CompareResultCell.Offset(0, 4).Value = "File Tag Only"
' move the cell one down
Set CompareResultCell = CompareResultCell.Offset(1, 0)
count = count + 1
Next FileOnlyTag
NextKeyCell:
' read the next key
Set Key = Key.Offset(1, 0)
Loop ' Do Until
' add border
With CompareResultRange()
With .Borders
.LineStyle = xlContinuous
End With
End With
ThisWorkbook.Sheets("Main").Range("C2").Select
If count > 0 Then
MsgBox (count & " mismatches found!")
Else
MsgBox ("Success: XMLs match!")
End If
End Sub