[SOAP] function to parse XML into an array
| From: | Paweł Krupa | Date: | Thu, 26 Nov 2009 13:11:15 +0000 |
| Subject: | [SOAP] function to parse XML into an array | ||
| Groups: | php.soap | ||
| Request: | Send a blank email to php-soap+get-3015@lists.php.net to get a copy of this message | ||
Hi All,
this simple function parses XML data given in $S string
into $M array.
Any repetitive tags are indexed and accessible using M['tag'][1..n]
hope this makes your life easier ;)
Paweł
function ParseXML(&$M,$S) {
while(1) {
$a=strpos($S,"<");
if($a===false) { if(!is_array($M)) $M=$S; return; }
$b=strpos($S,">",$a);
$tag=substr($S,$a+1,$b-$a-1);
if($tag[0]=='?') { $S=substr($S,$b+1); continue; } //ignoring meta tags
$c=strpos($S,"</$tag>",$b);
$T=substr($S,$b+1,$c-$b-1);
$S=substr($S,$c+3+strlen($tag));
if(isset($M[$tag])) {
if(is_array($M[$tag])) $k=count($M[$tag]);
else { $tmp=$M[$tag]; $M[$tag]=array(); $M[$tag][0]=$tmp; $k=1;}
ParseXML($M[$tag][$k],$T);
} else
ParseXML($M[$tag],$T);} }