note 35726 added to function.xml-parse-into-struct

From: Date: Fri, 12 Sep 2003 09:01:41 +0000
Subject: note 35726 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-56362@lists.php.net to get a copy of this message
I've just debugged and optimized my xmlparser. gdemartini's xml-example generates this structure: stdClass Object ( [FORM] => stdClass Object ( [SECTION] => stdClass Object ( [NAME] => Data [EDITFIELD] => stdClass Object ( [LABEL] => Text: [NAME] => poll_text [LENGTH] => 255 [SIZE] => 56 [REQUIRED] => T ) [MEMOFIELD] => stdClass Object ( [LABEL] => Options [NAME] => options [COLS] => 56 [ROWS] => 5 [REQUIRED] => T ) ) ) ) the moldb example generates this structure: stdClass Object ( [MOLDB] => stdClass Object ( [MOLECULE] => Array ( [0] => stdClass Object ( [NAME] => Alanine [SYMBOL] => ala [CODE] => A [TYPE] => hydrophobic ) [1] => stdClass Object ( [NAME] => Lysine [SYMBOL] => lys [CODE] => K [TYPE] => charged ) ) ) ) here is the code. enjoy! class XMLParser { var $path; var $result; function cleanString($string) { return trim(str_replace("'", "'", $string)); } function XMLParser($encoding, $data) { $this->path = "\$this->result"; $this->index = 0; $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); xml_parser_free($xml_parser); } function startElement($parser, $tag, $attributeList) { $this->path .= "->".$tag; eval("\$data = ".$this->path.";"); if (is_array($data)) { $index = sizeof($data); $this->path .= "[".$index."]"; } else if (is_object($data)) { eval($this->path." = array(".$this->path.");"); $this->path .= "[1]"; } foreach($attributeList as $name => $value) eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';"); } function endElement($parser, $tag) { $this->path = substr($this->path, 0, strrpos($this->path, "->")); } function characterData($parser, $data) { if ($data = XMLParser::cleanString($data)) eval($this->path." = '$data';"); } } ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35726 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35726&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35726&report=yes

« previous php.notes (#56362) next »