import mtweb.0.4.1
[mtweb] / web / libs / sxml.php
1 <?php
2
3 /*
4
5   from :
6
7   http://www.shop24-7.info/32-0-simplexml-alternative-php4.html
8
9   ajout :
10
11 -  xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
12    dans la fonction parse($data) pour la prise en compte de la casse
13
14 -  if($attribs) $this->data['attrs'] = $attribs;
15    dans la fonction tag_open pour la prise en compte des attributs
16
17 */
18
19 class sxml
20 {
21     var $parser;
22     var $error_code;
23     var $error_string;
24     var $current_line;
25     var $current_column;
26     var $data;
27     var $datas;
28     function parse($data)
29     {
30 //        $this->parser = xml_parser_create('UTF-8');
31         $this->data = array();
32         $this->datas = array();
33         $this->parser = xml_parser_create();
34         xml_set_object($this->parser, $this);
35         xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
36         xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
37         xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
38         xml_set_character_data_handler($this->parser, 'cdata');
39         if (!xml_parse($this->parser, $data))
40         {
41             $this->data = array();
42             $this->error_code = xml_get_error_code($this->parser);
43             $this->error_string = xml_error_string($this->error_code);
44             $this->current_line = xml_get_current_line_number($this->parser);
45             $this->current_column = xml_get_current_column_number($this->parser);
46         }
47         else
48         {
49             $this->data = $this->data['subs'];
50         }
51         xml_parser_free($this->parser);
52     }
53
54     function tag_open($parser, $tag, $attribs)
55     {
56         $this->datas[] = &$this->data;
57         $this->data = &$this->data['subs'][$tag][];
58         if($attribs) $this->data['attrs'] = $attribs;
59
60     }
61
62     function cdata($parser, $cdata)
63     {
64         @$this->data['data'] .= $cdata;
65     }
66
67     function tag_close($parser, $tag)
68     {
69         $this->data =& $this->datas[count($this->datas)-1];
70         array_pop($this->datas);
71     }
72 }
73
74 ?>