summaryrefslogtreecommitdiffstats
path: root/code/xmlparser.inc.php
blob: 0666c5a8b6c11af1a4847bb74d8e2b9b52e77b89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?PHP
  class XMLParser {
    var $tags;
    var $depth;
    
    function ParseFile($filename) {
      $this->tags = array();
      $this->depth = 0;
      
      $parser = xml_parser_create();
      
      xml_set_object($parser, $this);
      xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
      
      xml_set_element_handler($parser, 'openTagHandler', 'closeTagHandler');
      xml_set_character_data_handler($parser, 'cdataHandler');
      
      $file = fopen($filename, 'r');
      
      while ($xml = fread($file, 4096)) {
        if(!xml_parse($parser, $xml, feof($file))) {
          $this->tags = null;
          break;
        }
      }
      
      fclose($file);
      
      xml_parser_free($parser);
      
      return $this->tags[0];
    }
    
    function ParseXML($xml) {
      $this->tags = array();
      $this->depth = 0;
      
      $parser = xml_parser_create();
      
      xml_set_object($parser, $this);
      xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
      
      xml_set_element_handler($parser, 'openTagHandler', 'closeTagHandler');
      xml_set_character_data_handler($parser, 'cdataHandler');
      
      if(!xml_parse($parser, $xml, true))
        $this->tags = null;
      
      xml_parser_free($parser);
      
      return $this->tags[0];
    }
    
    function FindTag($data, $tag, $start = 0) {
      for($i = $start; $i < count($data['children']); $i++) {
        if(!is_array($data['children'][$i])) continue;
        if($data['children'][$i]['tag'] == $tag) return $data['children'][$i];
      }
    }
    
    function openTagHandler($parser, $name, $attribs) {
      $children = &$this->tags[$this->depth-1]['children'];
      
      $children[count($children)-1] = trim($children[count($children)-1]);
      
      $this->tags[$this->depth] = array('tag' => $name,
                                        'attribs' => $attribs,
                                        'children' => array());
      
      $this->depth++;
    }
    
    function closeTagHandler($parser, $name) {
      $children = &$this->tags[$this->depth-1]['children'];
      
      $children[count($children)-1] = trim($children[count($children)-1]);
      
      $this->depth--;
      
      if($this->depth > 0)
        array_push($this->tags[$this->depth-1]['children'], $this->tags[$this->depth]);
    }
    
    function cdataHandler($parser, $data) {
      $children = &$this->tags[$this->depth-1]['children'];
      
      if(is_string($children[count($children)-1]))
        $children[count($children)-1] .= $data;
      elseif(trim($data) != '')
        array_push($children, $data);
    }
  }
  
  $GLOBALS['xmlparser'] = new XMLParser;
?>