Skip to content

Instantly share code, notes, and snippets.

@alexander-brett
Created September 2, 2014 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexander-brett/73924cab487d81491867 to your computer and use it in GitHub Desktop.
Save alexander-brett/73924cab487d81491867 to your computer and use it in GitHub Desktop.
xmlDiff
package xmlDiff;
use Data::Dumper;
use strict;
use warnings;
use v5.10;
$\ = "\n";
$/ = "\0";
sub parseXML {
my $self = shift;
my $xmlString = shift;
my $result = shift;
$xmlString =~ m/\s*(<\?.*?\?>)?\s*<(\w+)( [^>]*)?(?>\/>|>((?>(?0)+|[^<>]*))<\/\2>)\s*/s
or die "Invalid XML: " . $xmlString;
$result->{declaration} = $1;
$result->{tagName} = $2;
$result->{properties} = $3;
$result->{innerXML} = $4;
$result->{children} = ();
my @matches = $result->{innerXML} =~ m/(\s*<(\w+)(?: [^>]*)?(?>\/>|>(?>(?0)+|[^<>]*)<\/\2>)\s*)/sg;
foreach (@matches[grep {not $_ % 2} 0..$#matches]) {
my $child = {};
parseXML($_, $child);
push @{ $result->{children} }, $child;
}
return $result;
}
sub new () {
return bless {}, shift;
}
#while(<>){
# my $parsed = {};
# parseXML($_, $parsed);
# print length $parsed->{innerXML};
# print $#{ $parsed->{children}};
# print Dumper $parsed->{children}[0]{children};
#}
use strict;
use warnings;
use Test::More tests => 2;
BEGIN {
use_ok('xmlDiff');
ok(my $diff = xmlDiff->new());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment