1 | <?php |
---|
2 | /***************************************************************************\ |
---|
3 | Plugin : magusine |
---|
4 | Licence : GPL |
---|
5 | Auteurs : Stéphane Noël, Philippe Vanderlinden |
---|
6 | Infos : http://www.spip-contrib.net/Le-plugin-Magusine |
---|
7 | http://www.magunews.net/spip.php?rubrique645 |
---|
8 | |
---|
9 | $LastChangedRevision: 12345 $ |
---|
10 | $LastChangedBy: bubu $ |
---|
11 | $LastChangedDate: 2008-03-21 15:50:47 +0100 (ven, 21 mar 2008) $ |
---|
12 | \***************************************************************************/ |
---|
13 | |
---|
14 | // ne pas oublier de crediter |
---|
15 | |
---|
16 | class xmlParser{ |
---|
17 | var $xml_obj = null; |
---|
18 | var $output = array(); |
---|
19 | var $attrs; |
---|
20 | |
---|
21 | function xmlParser(){ |
---|
22 | $this->xml_obj = xml_parser_create(); |
---|
23 | xml_set_object($this->xml_obj,$this); |
---|
24 | xml_set_character_data_handler($this->xml_obj, 'dataHandler'); |
---|
25 | xml_set_element_handler($this->xml_obj, "startHandler", "endHandler"); |
---|
26 | } |
---|
27 | |
---|
28 | function parse($path){ |
---|
29 | if (!($fp = fopen($path, "r"))) { |
---|
30 | die("Cannot open XML data file: $path"); |
---|
31 | return false; |
---|
32 | } |
---|
33 | |
---|
34 | while ($data = fread($fp, 4096)) { |
---|
35 | if (!xml_parse($this->xml_obj, $data, feof($fp))) { |
---|
36 | die(sprintf("XML error: %s at line %d", |
---|
37 | xml_error_string(xml_get_error_code($this->xml_obj)), |
---|
38 | xml_get_current_line_number($this->xml_obj))); |
---|
39 | xml_parser_free($this->xml_obj); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | return true; |
---|
44 | } |
---|
45 | |
---|
46 | function startHandler($parser, $name, $attribs){ |
---|
47 | $_content = array(); |
---|
48 | $_content['name'] = $name; |
---|
49 | if(!empty($attribs)) |
---|
50 | $_content['attrs'] = $attribs; |
---|
51 | array_push($this->output, $_content); |
---|
52 | } |
---|
53 | |
---|
54 | function dataHandler($parser, $data){ |
---|
55 | if(!empty($data) && $data!="\n") { |
---|
56 | $_output_idx = count($this->output) - 1; |
---|
57 | $this->output[$_output_idx]['content'] .= $data; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | function endHandler($parser, $name){ |
---|
62 | if(count($this->output) > 1) { |
---|
63 | $_data = array_pop($this->output); |
---|
64 | $_output_idx = count($this->output) - 1; |
---|
65 | $add = array(); |
---|
66 | if(!$this->output[$_output_idx]['child']) |
---|
67 | $this->output[$_output_idx]['child'] = array(); |
---|
68 | array_push($this->output[$_output_idx]['child'], $_data); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | ?> |
---|