Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm a beginner to Perl and have to write a script to compare 2 XML files and highlight the differences. The problem thatI have is that the tags used are the same throughout the file

As seen from the file structure, there can be any number of elements within the <zone>. ...</zone> tab. I need to individually extract them and compare it against another file of the same structure. I've no idea how to go about this so ur help would be greatly apprecicated.

Thanks
FILE STRUCTURE:
===============

<zone> <zone abc> <vertex x= "332" y="425"> <vertex x= "332" y="425"> <vertex x= "3032" y="2425"> <vertex x= "3032" y="2425"> </zone abc> <zone category = "000" value = "text"> </zone category> </zone> <zone> <zone abc> <vertex x= "336" y="225"> <vertex x= "336" y="225"> <vertex x= "1332" y="4125"> <vertex x= "1332" y="4125"> </zone abc> <zone category = "001" value = "image"> </zone category> </zone>";

20040115 Edited by Corion: Fixed formatting

Replies are listed 'Best First'.
Re: Comparing XML files using Perl script
by bart (Canon) on Jan 15, 2004 at 09:50 UTC
    It looks to me like XML::SemanticDiff might be of use.

    In addition, I see XML::DifferenceMarkup on CPAN, too, but at first sight that looks to be more strict in what it considers a difference.

Re: Comparing XML files using Perl script
by Willard B. Trophy (Hermit) on Jan 15, 2004 at 15:34 UTC
    A non-Perl solution: normalize both files using Bob Lyons's XSLT normalizer, then run your text diff tool of choice on the files.

    Bob Lyons's code ensures that all whitespace is made equivalent, and sorts all attributes into alphabetical order. I've found this approach works well for small XML files, or files containing a few differences.

    --
    bowling trophy thieves, die!