1 |
tdb |
1.2 |
# |
2 |
|
|
# i-scream central monitoring system |
3 |
tdb |
1.3 |
# http://www.i-scream.org |
4 |
tdb |
1.2 |
# Copyright (C) 2000-2002 i-scream |
5 |
|
|
# |
6 |
|
|
# This program is free software; you can redistribute it and/or |
7 |
|
|
# modify it under the terms of the GNU General Public License |
8 |
|
|
# as published by the Free Software Foundation; either version 2 |
9 |
|
|
# of the License, or (at your option) any later version. |
10 |
|
|
# |
11 |
|
|
# This program is distributed in the hope that it will be useful, |
12 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
# GNU General Public License for more details. |
15 |
|
|
# |
16 |
|
|
# You should have received a copy of the GNU General Public License |
17 |
|
|
# along with this program; if not, write to the Free Software |
18 |
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
# |
20 |
|
|
|
21 |
|
|
# ----------------------------------------------------------- |
22 |
tdb |
1.1 |
# iscream::XMLParser |
23 |
tdb |
1.3 |
# http://www.i-scream.org |
24 |
tdb |
1.2 |
# |
25 |
|
|
# iscream::XMLParser decodes a string of XML into a hash in a |
26 |
|
|
# very similar way to the Java version in the i-scream CMS. |
27 |
|
|
# |
28 |
tdb |
1.3 |
# $Author: tdb $ |
29 |
|
|
# $Id: XMLParser.pm,v 1.2 2003/03/21 19:24:07 tdb Exp $ |
30 |
tdb |
1.2 |
#------------------------------------------------------------ |
31 |
tdb |
1.1 |
|
32 |
|
|
package iscream::XMLParser; |
33 |
|
|
|
34 |
|
|
use strict; |
35 |
|
|
use XML::Parser; |
36 |
|
|
|
37 |
|
|
my @tagstack; |
38 |
|
|
my %xmlhash; |
39 |
|
|
|
40 |
|
|
sub parse { |
41 |
|
|
my($xml) = @_; |
42 |
|
|
%xmlhash = (); |
43 |
|
|
|
44 |
|
|
my $parser = new XML::Parser; |
45 |
|
|
|
46 |
|
|
$parser->setHandlers(Char => \&char_handler, |
47 |
|
|
Start => \&start_handler, |
48 |
|
|
End => \&end_handler); |
49 |
|
|
|
50 |
|
|
eval { $parser->parse($xml) }; |
51 |
|
|
|
52 |
|
|
return ($@, %xmlhash); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
sub char_handler { |
56 |
|
|
my ($p, $value) = @_; |
57 |
|
|
&add_value($value); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
sub start_handler { |
61 |
|
|
shift; |
62 |
|
|
my($tag) = shift; |
63 |
|
|
push(@tagstack, $tag); |
64 |
|
|
my($name) = shift; |
65 |
|
|
my($val) = shift; |
66 |
|
|
while(defined $name && defined $val) { |
67 |
|
|
push(@tagstack, "attributes.$name"); |
68 |
|
|
&add_value($val); |
69 |
|
|
pop(@tagstack); |
70 |
|
|
$name = shift; |
71 |
|
|
$val = shift; |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
sub end_handler { |
76 |
|
|
pop(@tagstack); # safe to not check, parser will do it |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
sub add_value { |
80 |
|
|
my($value) = @_; |
81 |
|
|
my($tag) = ""; |
82 |
|
|
foreach my $item (@tagstack) { |
83 |
|
|
$tag = $tag . $item . "."; |
84 |
|
|
} |
85 |
|
|
$tag =~ s/^(.*)\.$/$1/; |
86 |
|
|
$xmlhash{$tag} = $value; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
1; |