function to parse XML into an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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);
 }
}


--
PHP Soap Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Kernel Newbies]     [PHP Database]     [Yosemite]

  Powered by Linux