note 101554 added to ref.xml

From: Date: Fri, 24 Dec 2010 08:19:48 +0000
Subject: note 101554 added to ref.xml
Groups: php.notes 
Request: Send a blank email to php-notes+get-175189@lists.php.net to get a copy of this message
Regarding my previous post I have recently discovered an error in the output when special characters such as an "é" is encountered. The cdata function had been update to correct this flaw. (Refer to the notes and usage in the previous post) <?php /** * Convert an xml file to an associative array (including the tag attributes): * * @param Str $xml file/string. */ class xmlToArrayParser { /** * The array created by the parser which can be assigned to a variable with: $varArr = $domObj->array. * * @var Array */ public $array; private $parser; private $pointer; /** * $domObj = new xmlToArrayParser($xml); * * @param Str $xml file/string */ public function __construct($xml) { $this->pointer =& $this->array; $this->parser = xml_parser_create("UTF-8"); xml_set_object($this->parser, $this); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false); xml_set_element_handler($this->parser, "tag_open", "tag_close"); xml_set_character_data_handler($this->parser, "cdata"); xml_parse($this->parser, ltrim($xml)); } private function tag_open($parser, $tag, $attributes) { $this->convert_to_array($tag, '_'); $idx=$this->convert_to_array($tag, 'cdata'); if(isset($idx)) { $this->pointer[$tag][$idx] = Array('@idx' => $idx,'@parent' => &$this->pointer); $this->pointer =& $this->pointer[$tag][$idx]; }else { $this->pointer[$tag] = Array('@parent' => &$this->pointer); $this->pointer =& $this->pointer[$tag]; } if (!empty($attributes)) { $this->pointer['_'] = $attributes; } } /** * Adds the current elements content to the current pointer[cdata] array. */ private function cdata($parser, $cdata) { if(isset($this->pointer['cdata'])) { $this->pointer['cdata'] .= $cdata;} else { $this->pointer['cdata'] = $cdata;} } private function tag_close($parser, $tag) { $current = & $this->pointer; if(isset($this->pointer['@idx'])) {unset($current['@idx']);} $this->pointer = & $this->pointer['@parent']; unset($current['@parent']); if(isset($current['cdata']) && count($current) == 1) { $current = $current['cdata'];} else if(empty($current['cdata'])) { unset($current['cdata']); } } /** * Converts a single element item into array(element[0]) if a second element of the same name is encountered. */ private function convert_to_array($tag, $item) { if(isset($this->pointer[$tag][$item])) { $content = $this->pointer[$tag]; $this->pointer[$tag] = array((0) => $content); $idx = 1; }else if (isset($this->pointer[$tag])) { $idx = count($this->pointer[$tag]); if(!isset($this->pointer[$tag][0])) { foreach ($this->pointer[$tag] as $key => $value) { unset($this->pointer[$tag][$key]); $this->pointer[$tag][0][$key] = $value; }}}else $idx = null; return $idx; } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 206.75.202.212 ---- Manual Page -- http://www.php.net/manual/en/ref.xml.php Edit -- https://master.php.net/note/edit/101554 Del: integrated -- https://master.php.net/note/delete/101554/integrated Del: useless -- https://master.php.net/note/delete/101554/useless Del: bad code -- https://master.php.net/note/delete/101554/bad+code Del: spam -- https://master.php.net/note/delete/101554/spam Del: non-english -- https://master.php.net/note/delete/101554/non-english Del: in docs -- https://master.php.net/note/delete/101554/in+docs Del: other reasons-- https://master.php.net/note/delete/101554 Reject -- https://master.php.net/note/reject/101554 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175189) next »