-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxml2json.pl
More file actions
executable file
·53 lines (46 loc) · 1.02 KB
/
xml2json.pl
File metadata and controls
executable file
·53 lines (46 loc) · 1.02 KB
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
#!/usr/bin/perl
use strict;
use XML::Simple;
use Data::Dumper;
use JSON;
use XML::LibXSLT;
use XML::LibXML;
use Getopt::Long;
$\ ="\n";
my %param = ();
my $filter = '\.xml$';
GetOptions(
'p=s' => \%param,
'filter|f=s' => \$filter
);
sub doit;
my $src = shift;
my $xsl = shift;
my $xslt = XML::LibXSLT->new();
my $style_src = XML::LibXML->load_xml(location=>$xsl, no_cdata=>1);
my $stylesheet = $xslt->parse_stylesheet($style_src);
if (-e $src and -f $src){ #if $src is a file
doit $src;
}elsif(-d $src){ #if $src is a directory
my $re = qr/$filter/;
opendir DIR, $src or die qq|Impossible to open $src|;
my @files = grep {/$re/} grep {-f qq|$src/$_|} readdir DIR;
closedir DIR;
foreach (@files){
my $in = qq|$src/$_|;
my $out = $in;
$out =~ s/$re/.json/;
open STDOUT, qq|>$out|; #redirect stdout
doit $in;
}
}
sub doit{
my $xml = shift;
eval{
my $results = $stylesheet->transform_file($xml,%param);
#print $results;
binmode STDOUT;
print $stylesheet->output_as_bytes($results);
};
warn $@ if $@;
}