[SOAP] function to parse XML into an array

[email protected] (Paweł Krupa)
Newsgroups php.soap
Message-ID <[email protected]>
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);
  }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.