reorganisation des dossiers
[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   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   
29     function parse($data){
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         $this->data = array();
41         $this->error_code = xml_get_error_code($this->parser);
42         $this->error_string = xml_error_string($this->error_code);
43         $this->current_line = xml_get_current_line_number($this->parser);
44         $this->current_column = xml_get_current_column_number($this->parser);
45       }
46       else{
47         $this->data = $this->data['subs'];
48       }
49       xml_parser_free($this->parser);
50     }
51   
52     function tag_open($parser, $tag, $attribs){
53       $this->datas[] = &$this->data;
54       $this->data = &$this->data['subs'][$tag][];
55       if($attribs) $this->data['attrs'] = $attribs;
56     }
57   
58     function cdata($parser, $cdata){
59       @$this->data['data'] .= $cdata;
60     }
61   
62     function tag_close($parser, $tag){
63       $this->data =& $this->datas[count($this->datas)-1];
64       array_pop($this->datas);
65     }
66
67   }
68
69 ?>