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

From: Date: Thu, 14 Aug 2008 15:45:12 +0000
Subject: note 85102 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-143501@lists.php.net to get a copy of this message
I wrote a simple function which totally flattens any xml structure. You can then directly get the data or use regular expression functions to fetch data from the output. E.g., if the input is <gallery xmlns='http://www.pspaeth.de/joomlaPlugins/psg'> <g imageDir='einsatzGallerie'></g> <caption for="f1.jpg"}This is picture number 1</caption> <caption for="f2.jpg"}This is picture number 2</caption> </g> </gallery> the output will be an array: ['.gallery0@xmlns']=> ".gallery0@xmlns=http://www.pspaeth.de/joomlaPlugins/psg" ['.gallery0.g0@imageDir']=> ".gallery0.g0@imageDir=einsatzGallerie" ['.gallery0.g0.caption0@for']=> ".gallery0.g0.caption0@for=f1.jpg" ['.gallery0.g0.caption0']=> ".gallery0.g0.caption0=This is picture number 1" ['.gallery0.g0.caption1@for']=> ".gallery0.g0.caption1@for=f2.jpg" ['.gallery0.g0.caption1']=> ".gallery0.g0.caption1=This is picture number 2" Here the code: <?php function _xmlToArray($xml) { $parser = xml_parser_create('UTF-8'); // UTF-8 or ISO-8859-1 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $xml, $values); xml_parser_free($parser); $return = array(); $stack = ''; $attrs = array(); $levelCounters = array(0); foreach($values as $val) { if($val['type'] == "open") { $ind = array_pop($levelCounters); $stack = $stack . "." . $val['tag'] . $ind; if ($val['attributes']) { foreach($val['attributes'] as $attrKey => $attrVal) $return[$stack . '@' . $attrKey] = $stack . '@' . $attrKey . "=" . $attrVal; } array_push($levelCounters,$ind+1); array_push($levelCounters,0); } elseif($val['type'] == "close") { $stack = substr($stack,0,strrpos($stack,'.')); } elseif($val['type'] == "complete") { $ind = array_pop($levelCounters); $stack = $stack . "." . $val['tag'] . $ind; if ($val['attributes']) { foreach($val['attributes'] as $attrKey => $attrVal) $return[$stack . '@' . $attrKey] = $stack . '@' . $attrKey . "=" . $attrVal; } $return[$stack] = $stack . "=" . $val['value']; $stack = substr($stack,0,strrpos($stack,'.')); array_push($levelCounters,$ind+1); } } return $return; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 77.20.241.72 ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- https://master.php.net/note/edit/85102 Del: integrated -- https://master.php.net/note/delete/85102/integrated Del: useless -- https://master.php.net/note/delete/85102/useless Del: bad code -- https://master.php.net/note/delete/85102/bad+code Del: spam -- https://master.php.net/note/delete/85102/spam Del: non-english -- https://master.php.net/note/delete/85102/non-english Del: in docs -- https://master.php.net/note/delete/85102/in+docs Del: other reasons-- https://master.php.net/note/delete/85102 Reject -- https://master.php.net/note/reject/85102 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143501) next »