in reply to XML::Dumper: Nifty or Naughty?

This is the source of the xml_compare sub:
sub xml_compare { # ============================================================ =item * xml_compare( $xml1, $xml2 ) - Compares xml for content Compares two dumped Perl data structures (that is, compares the xml) f +or identity in content. Use this function rather than perl's built-in str +ing comparison, especially when dealing with perl data that is memory-loca +tion dependent (which pretty much means all references). This function wil +l return true for any two perl data that are either deep clones of each other, or identical. This method is exported by default. =cut # ------------------------------------------------------------ my $xml1 = shift; my $xml2 = shift; $xml1 =~ s/(<[^>]*)\smemory_address="\dx[A-Za-z0-9]+"([^<]*>)/$1$2 +/g; $xml2 =~ s/(<[^>]*)\smemory_address="\dx[A-Za-z0-9]+"([^<]*>)/$1$2 +/g; $xml1 =~ s/(<[^>]*)\sdefined=\"false\"([^<]>)/$1$2/g; # For backwa +rds $xml2 =~ s/(<[^>]*)\sdefined=\"false\"([^<]>)/$1$2/g; # compatibil +ity $xml1 =~ s/<\?xml .*>//; # Ignore XML declaration $xml2 =~ s/<\?xml .*>//; $xml1 =~ s/<\!DOCTYPE perldata \[.*\]>//s; # Remove DTD $xml2 =~ s/<\!DOCTYPE perldata \[.*\]>//s; $xml1 =~ s/^\n//gm; # Remove empty newlines $xml2 =~ s/^\n//gm; return not( $xml1 cmp $xml2 ); }
What do you see if you compare $rexmlified and $ssxmlified yourself? They must look like simple textual xml don't they? You could try runnung it through the substitutions above and see where they differ.

Replies are listed 'Best First'.
Re: Re: XML::Dumper: Nifty or Naughty?
by wufnik (Friar) on May 09, 2003 at 14:03 UTC
    Jaap, thanks for your response.
    Here is what my debugger thinks of $ssxmlified. sorry about the name.
    $ssxmlified = "<perldata>\n <hashref memory_address=\"0x1b932f8\">\n <item key=\"ferret\">\n <arrayref memory_address=\"0x1b9f174\">\n <item key=\"0\">sredni</item>\n <item key=\"1\">vashtar</item>\n </arrayref>\n </item>\n <item key=\"hen\">anabaptist</item>\n </hashref>\n</perldata>\n";

    here is $rexmlified
    $rexmlified = "<perldata>\n <hashref memory_address=\"0x21e8434\">\n <item key=\"ferret\">\n <arrayref memory_address=\"0x329ac98\">\n <item key=\"0\">sredni</item>\n <item key=\"1\">vashtar</item>\n </arrayref>\n </item>\n <item key=\"hen\">anabaptist</item>\n </hashref>\n</perldata>\n";

    which look pretty much identical to me, except those memory addresses. i thought xml_compare ignored these. will delve into the sub itself. any further thoughts very welcome.
      XML::Dumper is indeed nifty, though to use xml_compare properly, you should not use the OO form

       $xdumper->xml_compare($this,$that);

      but just plain ol'

       xml_compare($this, $that);

      the functional way.

      no qualifiers necessary. some of xml::dumper's functions are available in both functional and oo forms, but sadly not xml_compare, and i presume xml_identity. perhaps the perldoc should be augmented a teensy bit on this? whatever, this doesn't cast any shadow on the module itself, which gets a big thumbs up from me anyway.

      hmmm, do i get experience points for answering my own question, or are they deducted?