Created
September 2, 2014 16:32
-
-
Save alexander-brett/73924cab487d81491867 to your computer and use it in GitHub Desktop.
xmlDiff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | |
#} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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