Created
July 7, 2011 14:04
-
-
Save salathe/1069582 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* LazyestXMLParser xml parser class | |
* | |
* @access public | |
*/ | |
class LazyestXMLParser { | |
var $arrOutput = array(); | |
var $resParser; | |
var $strXmlData; | |
/** | |
* LazyestXMLParser::parse() | |
* | |
* @param mixed $strInputXML | |
* @return | |
*/ | |
function parse($strInputXML) { | |
$this->resParser = xml_parser_create (); | |
xml_parser_set_option($this->resParser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); | |
xml_set_object($this->resParser,$this); | |
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed"); | |
xml_set_character_data_handler($this->resParser, "tagData"); | |
$f = fopen( $strInputXML, 'r' ); | |
$readok = true; | |
while( ( $data = fread( $f, 4096 ) ) && $readok ) { | |
$this->strXmlData = xml_parse($this->resParser,$data ); | |
if(!$this->strXmlData) { | |
printf("XML error: %s at line %d in file %s <br />" , | |
xml_error_string(xml_get_error_code($this->resParser)), | |
xml_get_current_line_number($this->resParser), | |
$strInputXML ); | |
$readok = false; | |
} | |
} | |
xml_parser_free($this->resParser); | |
if ( $readok ) { | |
return $this->arrOutput; | |
} else { | |
return null; | |
} | |
} | |
/** | |
* LazyestXMLParser::tagOpen() | |
* | |
* @param mixed $parser | |
* @param mixed $name | |
* @param mixed $attrs | |
* @return | |
*/ | |
function tagOpen($parser, $name, $attrs) { | |
$tag=array("name"=>$name,"attrs"=>$attrs); | |
array_push($this->arrOutput,$tag); | |
} | |
/** | |
* LazyestXMLParser::tagData() | |
* | |
* @param mixed $parser | |
* @param mixed $tagData | |
* @return | |
*/ | |
function tagData($parser, $tagData) { | |
if(trim($tagData)) { | |
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagdata'])) { | |
$this->arrOutput[count($this->arrOutput)-1]['tagdata'] .= $tagData; | |
} | |
else { | |
$this->arrOutput[count($this->arrOutput)-1]['tagdata'] = $tagData; | |
} | |
} | |
} | |
/** | |
* LazyestXMLParser::tagClosed() | |
* | |
* @param mixed $parser | |
* @param mixed $name | |
* @return | |
*/ | |
function tagClosed($parser, $name) { | |
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1]; | |
array_pop($this->arrOutput); | |
} | |
} // LazyestXMLParser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment