note 50628 deleted from ref.xml by bjori
| From: | bjori@php.net | Date: | Mon, 09 Oct 2006 11:15:53 +0000 |
| Subject: | note 50628 deleted from ref.xml by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-118297@lists.php.net to get a copy of this message | ||
Note Submitter: pille at hbr1 dot com
----
A simple xml parser +regenerator class without handling attributes:
<?
class XMLSimpleParser {
function XMLSimpleParser($data,$encoding='') {
$this->document = new stdClass();
$this->current =& $this->document;
$xml_parser = xml_parser_create($encoding);
xml_set_object($xml_parser, &$this);
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
xml_set_character_data_handler($xml_parser, 'characterData');
xml_parse($xml_parser, $data, true);
$this->encoding = xml_parser_get_option( $xml_parser, XML_OPTION_TARGET_ENCODING );
xml_parser_free($xml_parser);
unset( $this->document->_ITEMS );
unset( $this->current );
}
function startElement($parser, $tag, $attributeList) {
if( is_object( $this->current->$tag ) ) {
$obj = $this->current->$tag;
$this->current->$tag = array();
array_push( $this->current->$tag, $obj );
}
if( is_array( $this->current->$tag ) ) {
$obj =& new stdClass;
$obj->_PARENT =& $this->current;
$obj->_ITEMS = 0;
array_push( $this->current->$tag, &$obj );
$this->current =& $obj;
}
else {
$this->current->$tag->_PARENT =& $this->current;
$this->current =& $this->current->$tag;
$this->current->_ITEMS = 0;
}
$this->current->_PARENT->_ITEMS ++;
}
function endElement($parser, $tag) {
$parent =& $this->current->_PARENT;
if( $this->current->_DATA != '' || $this->current->_ITEMS == 0 ) {
$this->current = $this->current->_DATA;
}
else {
unset( $this->current->_PARENT );
unset( $this->current->_ITEMS );
unset( $this->current->_DATA );
}
$this->current =& $parent;
}
function characterData($parser, $data) {
$this->current->_DATA = trim( $data );
}
function generateXML( $encoding = '' ) {
if( ! $encoding ) $encoding = $this->encoding;
$this->xml = '<?xml version="1.0"';
if( $encoding ) $this->xml .= ' encoding="' . $encoding . '"';
$this->xml .= "?>\n";
$this->xml .= $this->_generateXML( $this->document, 0 );
return $this->xml;
}
function _generateXML( $item, $level ) {
$xml = '';
if( is_object( $item ) ) {
$vars = get_object_vars( $item );
foreach( $vars as $key => $val ) {
if( is_array( $val ) ) {
foreach( $val as $entry ) {
for( $i = 0; $i < $level; $i ++ )
$xml .= "\t";
$xml .= '<' . $key;
if( $xml2 = $this->_generateXML( $entry, $level + 1 ) ) {
$xml .= ">\n" . $xml2;
for( $i = 0; $i < $level; $i ++ )
$xml .= "\t";
$xml .= '</' . $key . '>' . "\n";
}
else {
$xml .= " />\n";
}
}
}
else if( is_object( $val ) ) {
for( $i = 0; $i < $level; $i ++ )
$xml .= "\t";
$xml .= '<' . $key;
if( $xml2 = $this->_generateXML( $val, $level + 1 ) ) {
$xml .= ">\n" . $xml2;
for( $i = 0; $i < $level; $i ++ )
$xml .= "\t";
$xml .= '</' . $key . ">\n";
}
else {
$xml .= " />\n";
}
}
else {
for( $i = 0; $i < $level; $i ++ )
$xml .= "\t";
$xml .=
'<' . $key . '>' .
$val .
'</' . $key . ">\n";
}
}
}
return $xml;
}
}
?>