note 35726 deleted from function.xml-parse-into-struct by sobak

From: Date: Sat, 10 Dec 2016 23:26:53 +0000
Subject: note 35726 deleted from function.xml-parse-into-struct by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208468@lists.php.net to get a copy of this message
Note Submitter: Adam Tylmad ---- 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! <?php class XMLParser { var $path; var $result; function cleanString($string) { return trim(str_replace("'", "&#39;", $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';"); } } ?>

« previous php.notes (#208468) next »