note 50628 added to ref.xml

From: Date: Sat, 05 Mar 2005 01:52:33 +0000
Subject: note 50628 added to ref.xml
Groups: php.notes 
Request: Send a blank email to php-notes+get-85957@lists.php.net to get a copy of this message
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; } } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.xml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50628 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50628&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50628&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50628&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50628&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50628&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50628&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85957) next »