Loading rico1 and rico3 files
[infodrom/rico3] / plugins / php / class.xml.parser.php
1 <?php
2 //============================================================================
3 //============================================================================
4 // Script:      PHP Class "xmlParser"
5 //============================================================================
6 // From:        http://ch2.php.net/xml
7 // Autor:       monte at NOT-SP-AM dot ohrt dot com
8 // Date:        14-Sep-2005 06:48 
9 // License/
10 // Usage:       Open Source / for free  
11 //============================================================================
12 // DESCRIPTION:
13 // This is a class for XML parsing with an URL input. It does:
14 // -  Get File from URL (XML/RSS-File)
15 // -  Parsing the file into array
16 // -  Return Array
17 //============================================================================
18 //============================================================================
19
20
21 class xmlParser{
22
23 // *** ----------------------------------------------------------------
24 // DECLARATION
25 var $xml_obj = null;
26 var $output = array();
27
28
29 // *** ----------------------------------------------------------------
30 // CONSTRUCTOR
31 function xmlParser(){
32
33 $this->xml_obj = xml_parser_create();
34 xml_set_object($this->xml_obj,$this);
35 xml_set_character_data_handler($this->xml_obj, 'dataHandler'); 
36 xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
37
38
39
40
41 // *** ----------------------------------------------------------------
42 function parse($path){
43
44 if (!($fp = fopen($path, "r"))) {
45 die("Cannot open XML data file: $path");
46 return false;
47 }
48
49 while ($data = fread($fp, 4096)) {
50 if (!xml_parse($this->xml_obj, $data, feof($fp))) {
51 die(sprintf("XML error: %s at line %d",
52 xml_error_string(xml_get_error_code($this->xml_obj)),
53 xml_get_current_line_number($this->xml_obj)));
54 xml_parser_free($this->xml_obj);
55 }
56 }
57
58 return true;
59 }
60
61
62 // *** ----------------------------------------------------------------
63 function startHandler($parser, $name, $attribs){
64 $_content = array('name' => $name);
65 if(!empty($attribs))
66 $_content['attrs'] = $attribs;
67 array_push($this->output, $_content);
68 }
69
70 // *** ----------------------------------------------------------------
71 function dataHandler($parser, $data){
72 if(!empty($data)) {
73 $_output_idx = count($this->output) - 1;
74 $this->output[$_output_idx]['content'] = $data;
75 }
76 }
77
78 // *** ----------------------------------------------------------------
79 function endHandler($parser, $name){
80 if(count($this->output) > 1) {
81 $_data = array_pop($this->output);
82 $_output_idx = count($this->output) - 1;
83 $this->output[$_output_idx]['child'][] = $_data;
84
85 }
86
87 // *** ----------------------------------------------------------------
88 function GetNodeByPath($path,$tree = false) {
89 if ($tree) {
90 $tree_to_search = $tree;
91 }
92 else {
93 $tree_to_search = $this->output;
94 }
95
96 if ($path == "") {
97 return null; 
98 }
99
100 $arrPath = explode('/',$path);
101
102 foreach($tree_to_search as $key => $val) {
103 if (gettype($val) == "array") {
104 $nodename = $val[name];
105
106 if ($nodename == $arrPath[0]) { 
107
108 if (count($arrPath) == 1) { 
109 return $val;
110
111
112 array_shift($arrPath);
113
114 $new_path = implode($arrPath,"/");
115
116 return $this->GetNodeByPath($new_path,$val[child]);
117 }
118 }
119 }
120 }
121 } // class : end
122 ?>