vpavan77
asked on
perl script to compare two xml files
perl script to compare two xml files
appreciate if someone tell me how to write a perl script to compare two xml files and difference out the mismatched elements into another file
appreciate if someone tell me how to write a perl script to compare two xml files and difference out the mismatched elements into another file
ASKER
Thanks for the inputs... I am adding two xml files for reference
I want to compare these two xml files and diff out to a plain text with the missing keys in the file
For example - In File1.xml there are actually 6 elements(keys) and in the File2.xml there are only 5 elements(keys). The missing one is <translation lookup="label.registration .enterinfo "> This missing should be printed to another file say "differeences.txt"
Appreciate your quick response
----------
File1.xml
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------
<?xml version="1.0" encoding="UTF-8"?>
<translations language="ENG_US">
<translation lookup="label.login">Login </translat ion>
<translation lookup="label.cancel">Canc el</transl ation>
<translation lookup="label.username">Us er Name</translation>
<translation lookup="label.rememberme"> Remember Me</translation>
<translation lookup="label.password">Pa ssword</tr anslation>
<translation lookup="label.registration .enterinfo ">Enter your information</translation>
</translations>
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------
File2.xml below
------------
<?xml version="1.0" encoding="UTF-8"?>
<translations language="GER_DE">
<translation lookup="label.login">Anmel den</trans lation>
<translation lookup="label.cancel">Abbr echen</tra nslation>
<translation lookup="label.username">Be nutzername </translat ion>
<translation lookup="label.rememberme"> Speichern< /translati on>
<translation lookup="label.password">Pa sswort</tr anslation>
</translations>
I want to compare these two xml files and diff out to a plain text with the missing keys in the file
For example - In File1.xml there are actually 6 elements(keys) and in the File2.xml there are only 5 elements(keys). The missing one is <translation lookup="label.registration
Appreciate your quick response
----------
File1.xml
--------------------------
<?xml version="1.0" encoding="UTF-8"?>
<translations language="ENG_US">
<translation lookup="label.login">Login
<translation lookup="label.cancel">Canc
<translation lookup="label.username">Us
<translation lookup="label.rememberme">
<translation lookup="label.password">Pa
<translation lookup="label.registration
</translations>
--------------------------
File2.xml below
------------
<?xml version="1.0" encoding="UTF-8"?>
<translations language="GER_DE">
<translation lookup="label.login">Anmel
<translation lookup="label.cancel">Abbr
<translation lookup="label.username">Be
<translation lookup="label.rememberme">
<translation lookup="label.password">Pa
</translations>
I wan't aware of the xmldiff program... will it do what you need?
ASKER
I don't have any program on hand and am very new to Perl. I want a perl script which does the above requirement. Appreciate if you can help.
use XML::SemanticCompare;
my $x = XML::SemanticCompare->new;
#NOTE: Change the filenames as needed
local $/;
open(my $in, "<file1.xml") or die "File1: $!\n";
my $xml1=<$in>;
close($in);
open($in, "<file2.xml") or die "File2: $!\n";
my $xml2=<$in>;
close($in);
open(my $out, ">output.txt") or die "Output: $!\n";
# get the diffs
my $diffs_arrayref = $x->diff($control_xml, $test_xml);
print $out @$diffs_arrayref;
close($out);
ASKER
Thanks for the code... whe I run the code, I got the following error
Can't locate XML/SemanticCompare.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .)
Can't locate XML/SemanticCompare.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What differences do you care about?
Same tags, but in different order?
Tag content?
Tag attributes?
If you could post 2 sample files, and the sample output, it'll be easier to get what you want.