| <?php |
| |
| // This class handles the parsing for the eclipse-project-info.xml files |
| |
| class XMLParser{ |
| var $_xml_obj = null; |
| var $_output = array(); |
| |
| function XMLParser(){ |
| $this->_xml_obj = xml_parser_create(); |
| xml_set_object($this->_xml_obj,$this); |
| xml_set_character_data_handler($this->_xml_obj, 'dataHandler'); |
| xml_set_element_handler($this->_xml_obj, "startHandler", "endHandler"); |
| } |
| |
| function parse($path){ |
| if (!($fp = fopen($path, "r"))) { |
| die("Cannot open XML data file: $path"); |
| return false; |
| } |
| while($data = fread($fp, 4096)){ |
| if (!xml_parse($this->_xml_obj, $data, feof($fp))){ |
| die(sprintf("XML error: %s at line %d", |
| xml_error_string(xml_get_error_code($this->_xml_obj)), |
| xml_get_current_line_number($this->_xml_obj))); |
| xml_parser_free($this->_xml_obj); |
| } |
| } |
| fclose($fp); |
| |
| return true; |
| } |
| |
| function startHandler($parser, $name, $attribs){ |
| $_content = array('name' => $name); |
| if (!empty($attribs)) |
| $_content['attrs'] = $attribs; |
| array_push($this->_output, $_content); |
| } |
| |
| function dataHandler($parser, $data){ |
| if (!empty($data)){ |
| $_output_idx = count($this->_output) - 1; |
| $this->_output[$_output_idx]['content'] .= $data; |
| } |
| } |
| |
| function endHandler($parser, $name){ |
| if (count($this->_output) > 1){ |
| $_data = array_pop($this->_output); |
| $_output_idx = count($this->_output) - 1; |
| $this->_output[$_output_idx]['child'][] = $_data; |
| } |
| } |
| |
| function getOutput(){ |
| return $this->_output; |
| } |
| } |
| |
| ?> |