|
parser.inc.php |
Top Previous Next |
|
Simple PHP routines to parse XML.
<?
class CXml { var $pointer; var $index;
function CXml($xml_data = false) { if ($xml_data) $this->Set_xml_data( $xml_data ); unset($this->pointer); unset($this->index); }
function Set_xml_data( &$xml_data ) { $this->index = 0; $this->pointer[] = &$this;
$parser = xml_parser_create( "UTF-8" );
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false ); xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1); xml_set_object( $parser, $this ); xml_set_element_handler( $parser, "_startElement", "_endElement");
//xml_parse( $parser, $xml_data, true );
if (!xml_parse( $parser, $xml_data, true )) { die(sprintf("XML error: %s at line %d pos %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser), xml_get_current_column_number($parser))); } xml_parser_free( $parser ); }
function _startElement( $parser, $tag, $attributeList ) { foreach( $attributeList as $name => $value ) { $value = $this->_cleanString( $value ); $object->$name = $value; } //replaces the special characters with the underscore (_) in tag name $tag = preg_replace("/[:\-\. ]/", "_", $tag); eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" ); eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" ); eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" ); $this->index++; }
function _endElement( $parser, $tag ) { array_pop( $this->pointer ); $this->index--; }
function _cleanString( $string ) { return utf8_decode( trim( $string ) ); } }
?> |