note 111916 added to class.simplexmliterator

From: Date: Thu, 11 Apr 2013 04:34:16 +0000
Subject: note 111916 added to class.simplexmliterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-194447@lists.php.net to get a copy of this message
Most of the time we need to convert the XML to array or JSON , but now I have to completed the requirement of converting XML to XPath which make our template easily geting the data from XML data source due to the Xpath mapping .Here is the function: <?php function sxiToXpath($sxi, $key = null, &$tmp = null) { $keys_arr = array(); //get the keys count array for ($sxi->rewind(); $sxi->valid(); $sxi->next()) { $sk = $sxi->key(); if (array_key_exists($sk, $keys_arr)) { $keys_arr[$sk]+=1; $keys_arr[$sk] = $keys_arr[$sk]; } else { $keys_arr[$sk] = 1; } } //create the xpath for ($sxi->rewind(); $sxi->valid(); $sxi->next()) { $sk = $sxi->key(); if (!isset($$sk)) { $$sk = 1; } if ($keys_arr[$sk] >= 1) { $spk = $sk . '[' . $$sk . ']'; $keys_arr[$sk] = $keys_arr[$sk] - 1; $$sk++; } else { $spk = $sk; } $kp = $key ? $key . '/' . $spk : '/' . $sxi->getName() . '/' . $spk; if ($sxi->hasChildren()) { sxiToXpath($sxi->getChildren(), $kp, $tmp); } else { $tmp[$kp] = strval($sxi->current()); } $at = $sxi->current()->attributes(); if ($at) { $tmp_kp = $kp; foreach ($at as $k => $v) { $kp .= '/@' . $k; $tmp[$kp] = $v; $kp = $tmp_kp; } } } return $tmp; } function xmlToXpath($xml) { $sxi = new SimpleXmlIterator($xml); return sxiToXpath($sxi); } /** * How to use the function */ $xml = <<<EOT <?xml version="1.0" encoding="utf8" ?> <data> <item ID="30001"> <Company>Navarro Corp.</Company> </item> <item ID="30002" IDd="30002"> <Company>Performant Systems</Company> </item> <item ID="30003"> <Company id='id_test'><g id='id_g'>glove</g></Company> </item> <item> </item> </data> EOT; $rs = xmlToXpath($xml); print_r($rs); /** * the results may lkie this: Array ( [/data/item[1]/Company[1]] => Navarro Corp. [/data/item[1]/@ID] => SimpleXMLIterator Object ( [0] => 30001 ) [/data/item[2]/Company[1]] => Performant Systems [/data/item[2]/@ID] => SimpleXMLIterator Object ( [0] => 30002 ) [/data/item[2]/@IDd] => SimpleXMLIterator Object ( [0] => 30002 ) [/data/item[3]/Company[1]/g[1]] => glove [/data/item[3]/Company[1]/g[1]/@id] => SimpleXMLIterator Object ( [0] => id_g ) [/data/item[3]/Company[1]/@id] => SimpleXMLIterator Object ( [0] => id_test ) [/data/item[3]/@ID] => SimpleXMLIterator Object ( [0] => 30003 ) [/data/item[4]] => ) */ echo "Total:" . count($rs); echo "<hr>"; /* You can check the xpath result like this */ $xml = new SimpleXMLElement($xml); foreach ($rs as $k => $v) { echo "Xpath:" . $k . " |Value:" . $v . " "; var_dump($xml->xpath($k)); echo "<br>"; } ---- Server IP: 69.147.83.199 Probable Submitter: 182.92.247.1 ---- Manual Page -- http://php.net/manual/en/class.simplexmliterator.php Edit -- https://master.php.net/note/edit/111916 Del: integrated -- https://master.php.net/note/delete/111916/integrated Del: useless -- https://master.php.net/note/delete/111916/useless Del: bad code -- https://master.php.net/note/delete/111916/bad+code Del: spam -- https://master.php.net/note/delete/111916/spam Del: non-english -- https://master.php.net/note/delete/111916/non-english Del: in docs -- https://master.php.net/note/delete/111916/in+docs Del: other reasons-- https://master.php.net/note/delete/111916 Reject -- https://master.php.net/note/reject/111916 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194447) next »