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