1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#!/usr/bin/perl
use strict;
use warnings;
use XML::DT;
my $xmlfile = shift;
die "No file supplied\n" unless $xmlfile && -f $xmlfile;
my %tags;
my $code = dt( $xmlfile,
-pcdata => sub {
return "" if $c =~ m/^[\s\n\r]+$/;
$c =~ s/"/\\"/g;
$c =~ s/\$/\\\$/g;
'"' . $c . '",';
},
-default => sub {
if ($q =~ /[.:]/) {
my $otag = "<$q";
if (%v) {
for my $key (keys %v) {
$otag.= " $key='$v{$key}'";
}
}
$otag.=">";
return '"'.$otag.'",' . $c . "\"</$q>\""
} else {
$tags{$q}++;
my $attrs = "";
if (%v) {
$attrs = '{'.join(",",map {
$v{$_} =~ s/"/\\"/g;
$v{$_} =~ s/\$/\\\$/g;
"'$_'=>\"$v{$_}\""
} keys %v).'},';
}
"$q($attrs$c),"
}
});
$code =~ s/"",//g;
$code =~ s/,\)/)/g;
$code =~ s/,([a-zA-Z])/,\n\t$1/g;
binmode(STDOUT, ":utf8");
my $tags = join(" ", sort keys %tags);
print <<"EOP";
#!/usr/bin/perl
use utf8;
use strict;
use warnings;
use XML::Writer::Simple tags => [qw/$tags/];
binmode(STDOUT, ":utf8");
print xml_header(encoding => 'UTF-8');
print $code;
EOP
|