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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
package WWW::FairViewer::ParseXML;
use utf8;
use 5.014;
use warnings;
=encoding utf8
=head1 NAME
WWW::FairViewer::ParseXML - Convert XML to a HASH ref structure.
=head1 SYNOPSIS
Parse XML content and return an HASH ref structure.
Usage:
use WWW::FairViewer::ParseXML;
my $hash_ref = WWW::FairViewer::ParseXML::xml2hash($xml_string);
=head1 SUBROUTINES/METHODS
=head2 xml2hash($xml_string)
Parse XML and return an HASH ref.
=cut
sub xml2hash {
my $xml = shift() // return;
$xml = "$xml"; # copy the string
my $xml_ref = {};
my %args = (
attr => '-',
text => '#text',
empty => q{},
@_
);
my %ctags;
my $ref = $xml_ref;
state $inv_chars = q{!"#$@%&'()*+,/;\\<=>?\]\[^`{|}~};
state $valid_tag = qr{[^\-.\s0-9$inv_chars][^$inv_chars\s]*};
{
if (
$xml =~ m{\G< \s*
($valid_tag) \s*
((?>$valid_tag\s*=\s*(?>".*?"|'.*?')|\s+)+)? \s*
(/)?\s*> \s*
}gcsxo
) {
my ($tag, $attrs, $closed) = ($1, $2, $3);
if (defined $attrs) {
push @{$ctags{$tag}}, $ref;
$ref =
ref $ref eq 'HASH'
? ref $ref->{$tag}
? $ref->{$tag}
: (
defined $ref->{$tag}
? ($ref->{$tag} = [$ref->{$tag}])
: ($ref->{$tag} //= [])
)
: ref $ref eq 'ARRAY' ? ref $ref->[-1]{$tag}
? $ref->[-1]{$tag}
: (
defined $ref->[-1]{$tag}
? ($ref->[-1]{$tag} = [$ref->[-1]{$tag}])
: ($ref->[-1]{$tag} //= [])
)
: [];
++$#{$ref} if ref $ref eq 'ARRAY';
while (
$attrs =~ m{\G
($valid_tag) \s*=\s*
(?>
"(.*?)"
|
'(.*?)'
) \s*
}gsxo
) {
my ($key, $value) = ($1, $+);
$key = join(q{}, $args{attr}, $key);
if (ref $ref eq 'ARRAY') {
$ref->[-1]{$key} = _decode_entities($value);
}
elsif (ref $ref eq 'HASH') {
$ref->{$key} = $value;
}
}
if (defined $closed) {
$ref = pop @{$ctags{$tag}};
}
if ($xml =~ m{\G<\s*/\s*\Q$tag\E\s*>\s*}gc) {
$ref = pop @{$ctags{$tag}};
}
elsif ($xml =~ m{\G([^<]+)(?=<)}gsc) {
if (ref $ref eq 'ARRAY') {
$ref->[-1]{$args{text}} .= _decode_entities($1);
$ref = pop @{$ctags{$tag}};
}
elsif (ref $ref eq 'HASH') {
$ref->{$args{text}} .= $1;
$ref = pop @{$ctags{$tag}};
}
}
}
elsif (defined $closed) {
if (ref $ref eq 'ARRAY') {
if (exists $ref->[-1]{$tag}) {
if (ref $ref->[-1]{$tag} ne 'ARRAY') {
$ref->[-1]{$tag} = [$ref->[-1]{$tag}];
}
push @{$ref->[-1]{$tag}}, $args{empty};
}
else {
$ref->[-1]{$tag} = $args{empty};
}
}
}
else {
if ($xml =~ /\G(?=<(?!!))/) {
push @{$ctags{$tag}}, $ref;
$ref =
ref $ref eq 'HASH'
? ref $ref->{$tag}
? $ref->{$tag}
: (
defined $ref->{$tag}
? ($ref->{$tag} = [$ref->{$tag}])
: ($ref->{$tag} //= [])
)
: ref $ref eq 'ARRAY' ? ref $ref->[-1]{$tag}
? $ref->[-1]{$tag}
: (
defined $ref->[-1]{$tag}
? ($ref->[-1]{$tag} = [$ref->[-1]{$tag}])
: ($ref->[-1]{$tag} //= [])
)
: [];
++$#{$ref} if ref $ref eq 'ARRAY';
redo;
}
elsif ($xml =~ /\G<!\[CDATA\[(.*?)\]\]>\s*/gcs or $xml =~ /\G([^<]+)(?=<)/gsc) {
my ($text) = $1;
if ($xml =~ m{\G<\s*/\s*\Q$tag\E\s*>\s*}gc) {
if (ref $ref eq 'ARRAY') {
if (exists $ref->[-1]{$tag}) {
if (ref $ref->[-1]{$tag} ne 'ARRAY') {
$ref->[-1]{$tag} = [$ref->[-1]{$tag}];
}
push @{$ref->[-1]{$tag}}, $text;
}
else {
$ref->[-1]{$tag} .= _decode_entities($text);
}
}
elsif (ref $ref eq 'HASH') {
$ref->{$tag} .= $text;
}
}
else {
push @{$ctags{$tag}}, $ref;
$ref =
ref $ref eq 'HASH'
? ref $ref->{$tag}
? $ref->{$tag}
: (
defined $ref->{$tag}
? ($ref->{$tag} = [$ref->{$tag}])
: ($ref->{$tag} //= [])
)
: ref $ref eq 'ARRAY' ? ref $ref->[-1]{$tag}
? $ref->[-1]{$tag}
: (
defined $ref->[-1]{$tag}
? ($ref->[-1]{$tag} = [$ref->[-1]{$tag}])
: ($ref->[-1]{$tag} //= [])
)
: [];
++$#{$ref} if ref $ref eq 'ARRAY';
if (ref $ref eq 'ARRAY') {
if (exists $ref->[-1]{$tag}) {
if (ref $ref->[-1]{$tag} ne 'ARRAY') {
$ref->[-1] = [$ref->[-1]{$tag}];
}
push @{$ref->[-1]}, {$args{text} => $text};
}
else {
$ref->[-1]{$args{text}} .= $text;
}
}
elsif (ref $ref eq 'HASH') {
$ref->{$tag} .= $text;
}
}
}
}
if ($xml =~ m{\G<\s*/\s*\Q$tag\E\s*>\s*}gc) {
## tag closed - ok
}
redo;
}
elsif ($xml =~ m{\G<\s*/\s*($valid_tag)\s*>\s*}gco) {
if (exists $ctags{$1} and @{$ctags{$1}}) {
$ref = pop @{$ctags{$1}};
}
redo;
}
elsif ($xml =~ /\G<!\[CDATA\[(.*?)\]\]>\s*/gcs or $xml =~ m{\G([^<]+)(?=<)}gsc) {
if (ref $ref eq 'ARRAY') {
$ref->[-1]{$args{text}} .= $1;
}
elsif (ref $ref eq 'HASH') {
$ref->{$args{text}} .= $1;
}
redo;
}
elsif ($xml =~ /\G<\?/gc) {
$xml =~ /\G.*?\?>\s*/gcs or die "Invalid XML!";
redo;
}
elsif ($xml =~ /\G<!--/gc) {
$xml =~ /\G.*?-->\s*/gcs or die "Comment not closed!";
redo;
}
elsif ($xml =~ /\G<!DOCTYPE\s+/gc) {
$xml =~ /\G(?>$valid_tag|\s+|".*?"|'.*?')*\[.*?\]>\s*/sgco
or $xml =~ /\G.*?>\s*/sgc
or die "DOCTYPE not closed!";
redo;
}
elsif ($xml =~ /\G\z/gc) {
## ok
}
elsif ($xml =~ /\G\s+/gc) {
redo;
}
else {
die "Syntax error near: --> ", [split(/\n/, substr($xml, pos(), 2**6))]->[0], " <--\n";
}
}
return $xml_ref;
}
{
my %entities = (
'amp' => '&',
'quot' => '"',
'apos' => "'",
'gt' => '>',
'lt' => '<',
);
state $ent_re = do {
local $" = '|';
qr/&(@{[keys %entities]});/;
};
sub _decode_entities {
$_[0] =~ s/$ent_re/$entities{$1}/gor;
}
}
=head1 AUTHOR
Jesus, C<< <echo aGVja3llbEBoeXBlcmJvbGEuaW5mbw== | base64 -d> >>
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc WWW::FairViewer::ParseXML
=head1 LICENSE AND COPYRIGHT
Copyright 2012-2015 Trizen.
Copyright 2020 Jesus E.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See L<http://dev.perl.org/licenses/> for more information.
=cut
1; # End of WWW::FairViewer::ParseXML
|