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