1 |
tdb |
1.1 |
# iscream::XMLParser |
2 |
|
|
|
3 |
|
|
# iscream::XMLParser decodes a string of XML into a |
4 |
|
|
# has in a very similar way to the Java version in |
5 |
|
|
# the i-scream CMS. |
6 |
|
|
|
7 |
|
|
package iscream::XMLParser; |
8 |
|
|
|
9 |
|
|
use strict; |
10 |
|
|
use XML::Parser; |
11 |
|
|
|
12 |
|
|
my @tagstack; |
13 |
|
|
my %xmlhash; |
14 |
|
|
|
15 |
|
|
sub parse { |
16 |
|
|
my($xml) = @_; |
17 |
|
|
%xmlhash = (); |
18 |
|
|
|
19 |
|
|
my $parser = new XML::Parser; |
20 |
|
|
|
21 |
|
|
$parser->setHandlers(Char => \&char_handler, |
22 |
|
|
Start => \&start_handler, |
23 |
|
|
End => \&end_handler); |
24 |
|
|
|
25 |
|
|
eval { $parser->parse($xml) }; |
26 |
|
|
|
27 |
|
|
return ($@, %xmlhash); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
sub char_handler { |
31 |
|
|
my ($p, $value) = @_; |
32 |
|
|
&add_value($value); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
sub start_handler { |
36 |
|
|
shift; |
37 |
|
|
my($tag) = shift; |
38 |
|
|
push(@tagstack, $tag); |
39 |
|
|
my($name) = shift; |
40 |
|
|
my($val) = shift; |
41 |
|
|
while(defined $name && defined $val) { |
42 |
|
|
push(@tagstack, "attributes.$name"); |
43 |
|
|
&add_value($val); |
44 |
|
|
pop(@tagstack); |
45 |
|
|
$name = shift; |
46 |
|
|
$val = shift; |
47 |
|
|
} |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
sub end_handler { |
51 |
|
|
pop(@tagstack); # safe to not check, parser will do it |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
sub add_value { |
55 |
|
|
my($value) = @_; |
56 |
|
|
my($tag) = ""; |
57 |
|
|
foreach my $item (@tagstack) { |
58 |
|
|
$tag = $tag . $item . "."; |
59 |
|
|
} |
60 |
|
|
$tag =~ s/^(.*)\.$/$1/; |
61 |
|
|
$xmlhash{$tag} = $value; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
1; |