General discussion

Locked

How to Compare two XML Files in VB

By michaeltocik ·
What I would like some help on is How to compare two XML files with the same structure with each other to check for changes in the data, In Visual Basic 6.0. The actual situation is as follows We are dumping our data on another system but the other system requires that we only send through the changes in the actual data thus what we are trying to do is bring back a xml file for yesterday and for today and compare them for changes made to the actual data and only send through the changes.

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

How to Compare two XML Files in VB

Comparing two text files is relatively simple. You need to create:
two file handles for opening the files
two string variables to hold the data read from the files
You then read each file line by line into the strings and compare the strings to see if they're the same.
You could also create an output file for recording any differences if you feel it's necessary.

Example:
Filea.txt and Fileb.txt to be compared:

Public Function FileComp(strFileName1 as string, strFileName2 as string) as string

'create the file handles
dim fnum1 as long, fnum2 as long

'create the strings
dim str1 as string, str2 as string

'create return string and a line counter
dim strReturn as string
dim lngLine as long

'allocate the handles and open the files
fnum1 = freefile
open strFileName1 for input as fnum1
fnum2 = freefile
open strFileName2 for input as fnum2

'initialise line no to 0
lngLine = 0

'loop to end of file
while not eof(fnum1)

'add 1 to file num for each linelngLine = lngLine + 1

'read the data
line input #fnum1, str1
line input #fnum2, str2

'compare the variables
if str1 <> str2 then
'no match report error
strReturn = strReturn & _
"Difference at Line " & lngLine & vbCrlf & _
strFileName1 & ": " & str1 & vbCRlf &
sttFileName2 & ": " & str2 & vbcrlf & vbcrlf
end if

wend 'end of loop

'check for different File Sizes
if not eof(fnum2) then
'still data left in file 2
strReturn = strReturn & "Files are different sizes."
end if
'check if any differences
if len(strReturn) > 0 then
'differences detected
FileComp = strReturn
else
'no diff
FileComp = "Files Match"
end if

end function

Collapse -

How to Compare two XML Files in VB

1930 chars seems a lot but...
Note, the file length check is probably redundant if the files have the same structure but if there's a chance of file length (in lines) difference then you will need to have error handling to detect a shorter file2.
Hope this helps
John

Collapse -

How to Compare two XML Files in VB

by michaeltocik In reply to How to Compare two XML Fi ...

I am not comparing two text files I am compareing two XML files with nodes.

Collapse -

How to Compare two XML Files in VB

by michaeltocik In reply to How to Compare two XML Fi ...

Point value changed by question poster.

Collapse -

How to Compare two XML Files in VB

by michaeltocik In reply to How to Compare two XML Fi ...

Point value changed by question poster.

Collapse -

How to Compare two XML Files in VB

by b1xml2 In reply to How to Compare two XML Fi ...

This question has been asked occasionally in microsoft.public.xsl and microsoft.public.xml. The comparison of two XML Trees can be accomplished in basically ways:
1. Pure use of DOM Methods.
2. XML Transforms.


Method One - DOM Methods
------------------------
Whether you use VB or VBScript or JScript is really of no consequence. Comparing two Trees by itinerating through each Tree sounds yucky to me. A big no-no!!!

Method Two - Using XSLT
------------------------
The thing that you must consider is this:-
What type of comparison are you looking for
A --> B where B is being compared against A. The XML Tree returned will hold nodes in A not found in B.

A <-- B where A is being compared against B. The XML Tree returned willhold nodes in B not found in A.

A <--> B where A and B are being compared against each other. The XML Tree returned will hold nodes in B not found in A and vice-versa.

We make the grave assumption that position of each IXMLDOMElement within the Tree is important. However, this may not necessary be the case. For instance, the identity of a certain element at a certain depth in the Tree is defined by its id attribute. Thus, unless a clearer technical description of what needs to be comparedis given, you will just be clutching at straws.

Depending on your type of comparison, an XSLT document can be fashioned. Ultimately, you would still have to use a DOM method. In this case, transformNodeToObject()

Regards,

Brandon Driesen
Senior Analyst Programmer
XML/XSLT Guru

Collapse -

How to Compare two XML Files in VB

by michaeltocik In reply to How to Compare two XML Fi ...

Poster rated this answer

Collapse -

How to Compare two XML Files in VB

by michaeltocik In reply to How to Compare two XML Fi ...

This question was closed by the author

Back to Web Development Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums