We help IT Professionals succeed at work.

perl script to compare two xml files

vpavan77 asked
Medium Priority
Last Modified: 2020-05-13
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
Watch Question

Top Expert 2009

What format do you want the output?

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.


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
<?xml version="1.0" encoding="UTF-8"?>
<translations language="ENG_US">
  <translation lookup="label.login">Login</translation>
  <translation lookup="label.cancel">Cancel</translation>
  <translation lookup="label.username">User Name</translation>
  <translation lookup="label.rememberme">Remember Me</translation>
  <translation lookup="label.password">Password</translation>
  <translation lookup="label.registration.enterinfo">Enter your information</translation>
File2.xml below
<?xml version="1.0" encoding="UTF-8"?>
<translations language="GER_DE">
  <translation lookup="label.login">Anmelden</translation>
  <translation lookup="label.cancel">Abbrechen</translation>
  <translation lookup="label.username">Benutzername</translation>
  <translation lookup="label.rememberme">Speichern</translation>
  <translation lookup="label.password">Passwort</translation>
Top Expert 2009

I wan't aware of the xmldiff program... will it do what you need?


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.
Top Expert 2009


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>;
open($in, "<file2.xml") or die "File2: $!\n";
my $xml2=<$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;

Open in new window


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 .)
Top Expert 2009
You need to install that module.  If using ActiveState perl, then at a prompt:
    ppm install XML-SemanticCompare

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.