Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
<?php
2
 
3
/**
4
 * Project:     XMLParser: A library for parsing XML feeds
5
 * File:        XMLParser.class.php
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 *
21
 * @link http://www.phpinsider.com/php/code/XMLParser/
22
 * @copyright 2004-2005 New Digital Group, Inc.
23
 * @author Monte Ohrt <monte at newdigitalgroup dot com>
24
 * @package XMLParser
25
 * @version 1.0-dev
26
 */
27
 
28
 
29
class XMLParser{
30
 
31
   /**
32
    * holds the expat object
33
    *
34
    * @var obj
35
    */
36
   var $xml_obj = null;
37
 
38
   /**
39
    * holds the output array
40
    *
41
    * @var array
42
    */
43
   var $output = array();
44
 
45
   /**
46
    * the XML file character set
47
    *
48
    * @var array
49
    */
50
   var $char_set = 'UTF-8';
51
 
52
    /**#@-*/
53
    /**
54
     * The class constructor.
55
     */
56
   function __construct(){ }
57
 
58
 
59
    /**
60
     * parse the XML file (or URL)
61
     *
62
     * @param string $path the XML file path, or URL
63
     */
64
   function parse($path){
65
 
66
       $this->output = array();
67
 
68
       $this->xml_obj = xml_parser_create($this->char_set);
69
       xml_set_object($this->xml_obj,$this);
70
       xml_set_character_data_handler($this->xml_obj, 'dataHandler');
71
       xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
72
 
73
       if (!($fp = fopen($path, "r"))) {
74
           die("Cannot open XML data file: $path");
75
           return false;
76
       }
77
 
78
       while ($data = fread($fp, 4096)) {
79
           if (!xml_parse($this->xml_obj, $data, feof($fp))) {
80
               die(sprintf("XML error: %s at line %d",
81
               xml_error_string(xml_get_error_code($this->xml_obj)),
82
               xml_get_current_line_number($this->xml_obj)));
83
               xml_parser_free($this->xml_obj);
84
           }
85
       }
86
 
87
       return $this->output;
88
   }
89
 
90
    /**
91
     * define the start tag handler
92
     *
93
     * @param obj $parser the expat parser object
94
     * @param string $name the XML tag name
95
     * @param array $attribs the XML tag attributes
96
     */
97
   function startHandler($parser, $name, $attribs){
98
       $_content = array('name' => $name);
99
       if(!empty($attribs))
100
         $_content['attrs'] = $attribs;
101
       array_push($this->output, $_content);
102
   }
103
 
104
    /**
105
     * define the tag data handler
106
     *
107
     * @param obj $parser the expat parser object
108
     * @param string $data the XML data
109
     */
110
   function dataHandler($parser, $data){
111
       if(!empty($data)) {
112
           $_output_idx = count($this->output) - 1;
113
           if(!isset($this->output[$_output_idx]['content']))
114
             $this->output[$_output_idx]['content'] = $data;
115
           else
116
             $this->output[$_output_idx]['content'] .= $data;
117
       }
118
   }
119
 
120
    /**
121
     * define the end tag handler
122
     *
123
     * @param obj $parser the expat parser object
124
     * @param string $name the XML tag name
125
     */
126
   function endHandler($parser, $name){
127
       if(count($this->output) > 1) {
128
           $_data = array_pop($this->output);
129
           $_output_idx = count($this->output) - 1;
130
           $this->output[$_output_idx]['child'][] = $_data;
131
       }
132
   }
133
 
134
}
135
 
136
 
137
?>