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
10 // Usage: Open Source / for free
11 //============================================================================
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
17 //============================================================================
18 //============================================================================
23 // *** ----------------------------------------------------------------
26 var $output = array();
29 // *** ----------------------------------------------------------------
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");
41 // *** ----------------------------------------------------------------
42 function parse($path){
44 if (!($fp = fopen($path, "r"))) {
45 die("Cannot open XML data file: $path");
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);
62 // *** ----------------------------------------------------------------
63 function startHandler($parser, $name, $attribs){
64 $_content = array('name' => $name);
66 $_content['attrs'] = $attribs;
67 array_push($this->output, $_content);
70 // *** ----------------------------------------------------------------
71 function dataHandler($parser, $data){
73 $_output_idx = count($this->output) - 1;
74 $this->output[$_output_idx]['content'] = $data;
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;
87 // *** ----------------------------------------------------------------
88 function GetNodeByPath($path,$tree = false) {
90 $tree_to_search = $tree;
93 $tree_to_search = $this->output;
100 $arrPath = explode('/',$path);
102 foreach($tree_to_search as $key => $val) {
103 if (gettype($val) == "array") {
104 $nodename = $val[name];
106 if ($nodename == $arrPath[0]) {
108 if (count($arrPath) == 1) {
112 array_shift($arrPath);
114 $new_path = implode($arrPath,"/");
116 return $this->GetNodeByPath($new_path,$val[child]);