xml_parse_into_struct

(PHP 3 >= 3.0.8, PHP 4, PHP 5)

xml_parse_into_struct -- 将 XML 数据解析到数组中

说明

int xml_parse_into_struct ( resource parser, string data, array &values [, array &index] )

该函数将 XML 文件解析到两个对应的数组中,index 参数含有指向 values 数组中对应值的指针。最后两个数组参数可由指针传递给函数。

注: xml_parse_into_struct() 失败返回 0,成功返回 1。这和 FALSETRUE 不同,使用例如 === 的运算符时要注意。

以下范例显示了由该函数生成的数组的内部结构。我们简单地将一个 note 嵌入到一个 para 标记中,解析后我们可以打印出生成的数组的结构:

例子 1. xml_parse_into_struct() 示例

<?php
$simple
= "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo
"Index array\n";
print_r($index);
echo
"\nVals array\n";
print_r($vals);
?>

运行以上代码,我们得到的输出将是:

Index array
Array
(
    [PARA] => Array
        (
            [0] => 0
            [1] => 2
        )

    [NOTE] => Array
        (
            [0] => 1
        )

)

Vals array
Array
(
    [0] => Array
        (
            [tag] => PARA
            [type] => open
            [level] => 1
        )

    [1] => Array
        (
            [tag] => NOTE
            [type] => complete
            [level] => 2
            [value] => simple note
        )

    [2] => Array
        (
            [tag] => PARA
            [type] => close
            [level] => 1
        )

)

如果您的 XML 文档很复杂,基于该文档的事件处理(Event-driven)解析(基于 expat 扩展库)也会对应的变得复杂。该函数生成的并非 DOM 风格的对象,而是横向的树状结构。因此,我们能够方便的建立表达 XML 文件数据的对象。我们假设以下 XML 文件表示一个关于氨基酸信息的小型数据库:

例子 2. moldb.xml - 分子信息的小型数据库

<?xml version="1.0"?>
<moldb>

    <molecule>
        <name>Alanine</name>
        <symbol>ala</symbol>
        <code>A</code>
        <type>hydrophobic</type>
    </molecule>

    <molecule>
        <name>Lysine</name>
        <symbol>lys</symbol>
        <code>K</code>
        <type>charged</type>
    </molecule>

</moldb>
以下是解析该文档并生成相应对象的代码:

例子 3. parsemoldb.php - 将 moldb.xml 解析到分子(molecular)对象的数组中

<?php

class AminoAcid {
    var
$name;  // aa 姓名
    
var $symbol;    // 三字母符号
    
var $code;  // 单字母代码
    
var $type;  // hydrophobic, charged 或 neutral

    
function AminoAcid ($aa)
    {
        foreach (
$aa as $k=>$v)
            
$this->$k = $aa[$k];
    }
}

function
readDatabase($filename)
{
    
// 读取 aminoacids 的 XML 数据
    
$data = implode("",file($filename));
    
$parser = xml_parser_create();
    
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, $data, $values, $tags);
    
xml_parser_free($parser);

    
// 遍历 XML 结构
    
foreach ($tags as $key=>$val) {
        if (
$key == "molecule") {
            
$molranges = $val;
            
// each contiguous pair of array entries are the
            // lower and upper range for each molecule definition
            
for ($i=0; $i < count($molranges); $i+=2) {
                
$offset = $molranges[$i] + 1;
                
$len = $molranges[$i + 1] - $offset;
                
$tdb[] = parseMol(array_slice($values, $offset, $len));
            }
        } else {
            continue;
        }
    }
    return
$tdb;
}

function
parseMol($mvalues)
{
    for (
$i=0; $i < count($mvalues); $i++) {
        
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
    }
    return new
AminoAcid($mol);
}

$db = readDatabase("moldb.xml");
echo
"** Database of AminoAcid objects:\n";
print_r($db);

?>
在执行完 parsemoldb.php 后,变量 $db 将包含有一个由 AminoAcid 对象组成的数组,该脚本的输出如下:

** Database of AminoAcid objects:
Array
(
    [0] => aminoacid Object
        (
            [name] => Alanine
            [symbol] => ala
            [code] => A
            [type] => hydrophobic
        )

    [1] => aminoacid Object
        (
            [name] => Lysine
            [symbol] => lys
            [code] => K
            [type] => charged
        )

)


add a note add a note User Contributed Notes
efredricksen at gmail dot com
24-May-2006 03:55
Perhaps the one true parser:? I modified xademax's fine code to tidy it up, codewise and style wise, rationalize some minor crazyness, and make names fit nomenclature from the XML spec. (There are no uses of eval, and shame on you people who do.)

<?php
class XmlElement {
  var
$name;
  var
$attributes;
  var
$content;
  var
$children;
};

function
xml_to_object($xml) {
 
$parser = xml_parser_create();
 
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, $tags);
 
xml_parser_free($parser);

 
$elements = array();  // the currently filling [child] XmlElement array
 
$stack = array();
  foreach (
$tags as $tag) {
  
$index = count($elements);
   if (
$tag['type'] == "complete" || $tag['type'] == "open") {
    
$elements[$index] = new XmlElement;
    
$elements[$index]->name = $tag['tag'];
    
$elements[$index]->attributes = $tag['attributes'];
    
$elements[$index]->content = $tag['value'];
     if (
$tag['type'] == "open") {  // push
      
$elements[$index]->children = array();
      
$stack[count($stack)] = &$elements;
      
$elements = &$elements[$index]->children;
     }
   }
   if (
$tag['type'] == "close") {  // pop
    
$elements = &$stack[count($stack) - 1];
     unset(
$stack[count($stack) - 1]);
   }
  }
  return
$elements[0];  // the single top-level element
}

// For example:
$xml = '
<parser>
   <name language="en-us">Fred Parser</name>
   <category>
       <name>Nomenclature</name>
       <note>Noteworthy</note>
   </category>
</parser>
'
;
print_r(xml_to_object($xml));
?>

will give:

xmlelement Object
(
   [name] => parser
   [attributes] =>
   [content] =>
   [children] => Array
       (
           [0] => xmlelement Object
               (
                   [name] => name
                   [attributes] => Array
                       (
                           [language] => en-us
                       )

                   [content] => Fred Parser
                   [children] =>
               )

           [1] => xmlelement Object
               (
                   [name] => category
                   [attributes] =>
                   [content] =>
                   [children] => Array
                       (
                           [0] => xmlelement Object
                               (
                                   [name] => name
                                   [attributes] =>
                                   [content] => Nomenclature
                                   [children] =>
                               )

                           [1] => xmlelement Object
                               (
                                   [name] => note
                                   [attributes] =>
                                   [content] => Noteworthy
                                   [children] =>
                               )

                       )

               )

       )

)
Stuart
24-May-2006 06:26
This is a great little function for a lot of XML work, but note that this function does not handle XML entities properly.

The basic XML entities &lt; &gt; &amp; &quot; are fine, just anything else will not work:

If the entity is defined in the XML header, the parser will drop it completely from the struct it creates.

If the entity is not defined in the XML then the parser will crash out with an undefined entity error.

You should be able to work around this limitation by using a preg_replace on your XML string before passing it to the parser.

(Further details in Bug #35271; this is just a warning to those thinking of using this function for parsing real XML documents not just trivial XML examples)
donna at coloma dot com
29-Apr-2006 03:15
I needed a very simple parser for a set of name-value pairs to be stored in a single database field. I started with the mold example, paired it down, picked up the "id" attributes. Perhaps it will be useful for someone else.

<?php
/* simple conversion for name-value fields */

$xmlInput = "<?xml version=\"1.0\"?>
<mcw_settings>
  <field id=\"imageAlign\">left</field>
  <field id=\"caption\">What a nice picture.</field>
</mcw_settings>"
;

$desiredResult = array (
  
'imageAlign' => "left",
  
'caption' => "What a nice picture."
);

function
parseFields ($data)
{
  
// read the XML database of fields
  
$parser = xml_parser_create();
  
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, $data, $values, $tags);
  
xml_parser_free($parser);
  
$fields = null;

  
// loop through the structures
  
$fieldIndices = $tags['field'];
   for (
$i=0; $i < count($fieldIndices); $i++ ) {
      
$fieldInfo = $values[$fieldIndices[$i]];
      
$fields[$fieldInfo['attributes']['id']] = $fieldInfo['value'];
   }
   return
$fields;
}

$test = parseFields($xmlInput);
echo
"** Result:\n";
print_r($test);
?>
matt at australiangamer dot com
03-Apr-2006 10:58
I liked VampBoy's code as it gave me structure pretty much as I wanted it. Just two notes, though:

There is a bug in this code. subdivide() checks for pre-existing values, but not in the case of a "complete" element.

In the following (appallingly bad) XML

<Names>
<Name>Matt</Name>
<Name>Stacy</Name>
<Names>

Only Stacy is added to Names in the array, wiping Matt in the process.

To fix this replace the following code
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
  
$newarray[$dat[tag]]=$dat[value];
}
?>
with
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
     if (isset(
$newarray[$dat['tag']]) && is_array($newarray[$dat['tag']])){
        
$newarray[$dat['tag']][] = $dat['value'];
     } elseif (isset(
$newarray[$dat['tag']]) && !is_array($newarray[$dat['tag']])){
        
$newarray[$dat['tag']] = array($newarray[$dat['tag']], $dat['value']);
     } else {
        
$newarray[$dat['tag']]=$dat['value'];
     }
}
?>

Oh, also, note that the $dat[level], etc, WILL generate warnings, and should be more correctly written as $dat['level'].

The other thing I thought I should point out is that the array keys when created using xml_parse_into_struct will be an UPPERCASE version of your existing element names. If case is important, especially if you, like me, need mixedCase, do the following:

<?php
$xml_parser
= xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
?>
30-Mar-2006 03:57
$simple = '<?xml version="1.0"?>
<moldb>
   <molecule>
       <name>Alanine</name>
       <symbol>ala</symbol>
       <code>A</code>
       <type>hydrophobic</type>
   </molecule>
   <molecule>
       <name>Lysine</name>
       <symbol>lys</symbol>
       <code>K</code>
       <type>charged</type>
   </molecule>
</moldb>';

$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "<pre>";
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);

foreach ($vals as $k => $v) {
   $i = 0;
   if (trim($v['value']) != '') {
       if ($arr[$i][$v['tag']] != '') {
           $i ++;
       }
       $arr[$i][$v['tag']] = $v['value'];
   }
}
xademax at gmail dot com
09-Jan-2006 04:15
This is just another simple xml parser :)

<?php

class Xml
{
   var
$tag;
   var
$value;
   var
$attributes;
   var
$next;
}

function
xml2array($xml_string)
{
  
$Parser = xml_parser_create();
  
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_string, $Xml_Values);
  
xml_parser_free($Parser);
  
$XmlClass = array();
  
$LastObj = array();
  
$NowObj = &$XmlClass;

   foreach(
$Xml_Values as $Xml_Key => $Xml_Value)
   {
      
$Index = count($NowObj);
       if(
$Xml_Value["type"] == "complete")
       {
          
$NowObj[$Index] = new Xml;
          
$NowObj[$Index]->tag = $Xml_Value["tag"];
          
$NowObj[$Index]->value = $Xml_Value["value"];
          
$NowObj[$Index]->attributes = $Xml_Value["attributes"];
       }
       elseif(
$Xml_Value["type"] == "open")
       {
          
$NowObj[$Index] = new Xml;
          
$NowObj[$Index]->tag = $Xml_Value["tag"];
          
$NowObj[$Index]->value = $Xml_Value["value"];
          
$NowObj[$Index]->attributes = $Xml_Value["attributes"];
          
$NowObj[$Index]->next = array();
          
$LastObj[count($LastObj)] = &$NowObj;
          
$NowObj = &$NowObj[$Index]->next;
       }
       elseif(
$Xml_Value["type"] == "close")
       {
          
$NowObj = &$LastObj[count($LastObj) - 1];
           unset(
$LastObj[count($LastObj) - 1]);
       }
      
   }

   return
$XmlClass;
}

$String = "
<parser>
   <parseur_name>MyParser</parseur_name>
   <category>
       <name>Name 1</name>
       <note>A note 1</note>
   </category>
</parser>
"
;
$Xml = xml2array($String);

print_r($Xml);
?>

This exemple will show :
Array
(
   [0] => Xml Object
       (
           [tag] => parser
           [value] =>
           [attributes] =>
           [next] => Array
               (
                   [0] => Xml Object
                       (
                           [tag] => parseur_name
                           [value] => MyParser
                           [attributes] =>
                           [next] =>
                       )

                   [1] => Xml Object
                       (
                           [tag] => category
                           [value] =>
                           [attributes] =>
                           [next] => Array
                               (
                                   [0] => Xml Object
                                       (
                                           [tag] => name
                                           [value] => Name 1
                                           [attributes] =>
                                           [next] =>
                                       )

                                   [1] => Xml Object
                                       (
                                           [tag] => note
                                           [value] => A note 1
                                           [attributes] =>
                                           [next] =>
                                       )

                               )

                       )

               )

       )

)
VampBoy
15-Dec-2005 06:45
WHUPS! that was a broken test version.here is the real one:

class xml2array{
/* This class parses XML tags into a recursive, associative array with the tags as the associative array elements names.

if it encounters multiples of the same tag within a stream, it enumerates them as a sub array under the tag thus:

Array (
   [Lvl1tag] => Array (
       [0] => Array(
           [Lvl2tag] = "foo")
       [1]=> Array(
           [Lvl2tag] = "bar")
   )
)

It tries to detect when there is only one copy of a tag under another, and concatinate properly.
*/

   function readxmlfile($xmlfile){ // reads XML file in and returns it
     $xmlstream =fopen($xmlfile,r);
     $xmlraw=fread($xmlstream,1000000);
     fclose($xmlstream);
     return $xmlraw;
   }

   function parseXMLintoarray ($xmldata){ // starts the process and returns the final array
     $xmlparser = xml_parser_create();
     xml_parse_into_struct($xmlparser, $xmldata, $arraydat);
     xml_parser_free($xmlparser);
     $semicomplete = $this->subdivide($arraydat);
     $complete = $this->correctentries($semicomplete);
     return $complete;
   }
  
   function subdivide ($dataarray, $level = 1){
     foreach ($dataarray as $key => $dat){
       if ($dat[level] === $level && $dat[type] === "open"){
         $toplvltag = $dat[tag];
       } elseif ($dat[level] === $level && $dat[type] === "close" && $dat[tag]=== $toplvltag){
         $newarray[$toplvltag][] = $this->subdivide($temparray,($level +1));
        
        
         unset($temparray,$nextlvl);
       } elseif ($dat[level] === $level && $dat[type] === "complete"){
         $newarray[$dat[tag]]=$dat[value];
       } elseif ($dat[type] === "complete"||$dat[type] === "close"||$dat[type] === "open"){
         $temparray[]=$dat;
       }
     }
     return $newarray;
   }
function correctentries($dataarray){

if (is_array($dataarray)){
  $keys =  array_keys($dataarray);
  if (count($keys)== 1 && is_int($keys[0])){
   $tmp = $dataarray[0];
   unset($dataarray[0]);
       $dataarray = $tmp;
  }
  $keys2 = array_keys($dataarray);
  foreach($keys2 as $key){
   $tmp2 = $dataarray[$key];
   unset($dataarray[$key]);
   $dataarray[$key] = $this->correctentries($tmp2);
   unset($tmp2);
  }
  }
return $dataarray;
}
}
p dot gasiorowski at axent dot pl
13-Dec-2005 10:31
Something similar to kieran's _xml2array, however much more faster.
------
class ObjectFromXML
{
   var $parser;
   var $iter = 0;
   var $path = array();
   var $xml = array();

   function ObjectFromXML($XML)
   {
       $this->parser = xml_parser_create();
      
       xml_set_object($this->parser, &$this);
      
       xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
       xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);

       xml_set_element_handler($this->parser, "hanleTagStart", "hanleTagEnd");
       xml_set_character_data_handler($this->parser, "hanleTagCData");

       xml_parse($this->parser, $XML);
       xml_parser_free($this->parser);
      
       $this->xml = $this->xml['_children'][0];
   }

   function getEvalPath()
   {
       return '$this->xml[' . "'" . implode("']['", $this->path) . "'" . ']';
   }

   function hanleTagStart($parser, $tag, $attributes)
   {
       array_push($this->path, '_children');
       array_push($this->path, ($this->iter++));

       $e = $this->getEvalPath();
       eval ($e . "['_name'] = \$tag;");
       if ($attributes !== array())
       {
           eval ($e . "['_attributes'] = \$attributes;");
       }
   }

   function hanleTagCData($parser, $cdata)
   {
       $e = $this->getEvalPath();
       eval ($e . "['_value'] = \$cdata;");
   }

   function hanleTagEnd($parser, $tag)
   {
       array_pop($this->path);
       array_pop($this->path);
   }
}
mbirth at webwriters dot de
08-Nov-2005 07:48
Searching for a nice and working way to get a RSS feed into an array-structure, I found the solution posted by kieran but disliked those several eval()s. So I wrote my own using references/pointers.

<?php

class RSSParser {
 
  var
$struct = array();  // holds final structure
 
var $curptr// current branch on $struct
 
var $parents = array();  // parent branches of current branch
 
 
function RSSParser($url) {
  
$this->curptr =& $this->struct// set ref to base
  
$xmlparser = xml_parser_create();
  
xml_set_object($xmlparser, $this);
  
xml_set_element_handler($xmlparser, 'tag_open', 'tag_close');
  
xml_set_character_data_handler($xmlparser, 'cdata');
  
$fp = fopen($url, 'r');

   while (
$data = fread($fp, 4096))
    
xml_parse($xmlparser, $data, feof($fp))
     || die(
sprintf("XML error: %s at line %d",
        
xml_error_string(xml_get_error_code($xmlparser)),
        
xml_get_current_line_number($xmlparser)));

  
fclose($fp);
  
xml_parser_free($xmlparser);
  }
 
  function
tag_open($parser, $tag, $attr) {
  
$i = count($this->curptr['children']);
  
$j = count($this->parents);
  
$this->curptr['children'][$i]=array();  // add new child element
  
$this->parents[$j] =& $this->curptr// store current position as parent
  
$this->curptr =& $this->curptr['children'][$i];  // submerge to newly created child element
  
$this->curptr['name'] = $tag;
   if (
count($attr)>0) $this->curptr['attr'] = $attr;
  }
 
  function
tag_close($parser, $tag) {
  
$i = count($this->parents);
   if (
$i>0) $this->curptr =& $this->parents[$i-1];  // return to parent element
  
unset($this->parents[$i-1]);  // clear from list of parents
 
}
 
  function
cdata($parser, $data) {
  
$data = trim($data);
   if (!empty(
$data)) {
    
$this->curptr['value'] .= $data;
   }
  }
 
}

$myparser = new RSSParser('getitems.xml');
$anotherparser = new RSSParser('http://johndoe:secret@myfeeds.com/getfeed.xml');

print_r($myparser->struct);
print_r($anotherparser->struct);

?>
kieran at kieran dot ca
21-Oct-2005 12:20
<?
/*
|
| _xml2array - another abstraction layer on xml_parse_into_struct
|              that returns a nice nested array.
|
|      @param: $xml is a string containing a full xml document
|
|    returns: a nested php array that looks like this:
|                 
|              array
|              (
|                  [_name] => the name of the tag
|                  [_attributes] => an array of 'attribute'=>'value' combos
|                  [_value] => the text contents of the node
|                  [_children] => an array of these arrays, one for each node.
|              )
|
|      notes: thanks to 'jeffg at activestate dot com' who inspired
|              me to essentially re-write his example code from php.net
|
|          me: Kieran Huggins < kieran[at]kieran[dot]ca >
|
*/
function _xml2array($xml){
   global
$keys;
   global
$level;
   if(!
is_array($xml)){ // init on first run
      
$raw_xml = $xml;
      
$p = xml_parser_create();
      
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
      
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
      
xml_parse_into_struct($p, $raw_xml, $xml, $idx);
      
xml_parser_free($p);
   }
   for(
$i=0;$i<count($xml,1);$i++){
      
// set the current level
      
$level = $xml[$i]['level'];

       if(
$level<1)break;

      
// mark this level's tag in the array
      
$keys[$level] = '['.$i.']';
      
      
// if we've come down a level, sort output and destroy the upper level
      
if(count($keys)>$level) unset($keys[count($keys)]);

      
// ignore close tags, they're useless
      
if($xml[$i]['type']=="open" || $xml[$i]['type']=="complete"){

          
// build the evalstring
          
$e = '$output'.implode('[\'_children\']',$keys);

          
// set the tag name
          
eval($e.'[\'_name\'] = $xml[$i][\'tag\'];');

          
// set the attributes
          
if($xml[$i]['attributes']){
               eval(
$e.'[\'_attributes\'] = $xml[$i][\'attributes\'];');
           }
          
          
// set the value
          
if($xml[$i]['value']){
               eval(
$e.'[\'_value\'] = trim($xml[$i][\'value\']);');
           }

       }

   }

   return
$output;
}
?>
info at gramba dot tv
23-Aug-2005 11:54
I was working with the xml2array functions below and had big performance problems. I fired them on a 20MB XML file and had to quit since all approaches of parsing where just too slow (more than 20 Minute parsing etc..). The solution was parsing it manually with preg_match, which increased performance by more than 20 times (processing time about 1 minute).

Rough example function with high performance:

<?php

function customXMLtoARRAY($xmlstring) {

  
// get all nodes
  
preg_match_all("#<node>(.*?)</node>#s",$xmlstring,$nodes);
  
$xmlstring = NULL;

  
$allnodes = array();

  
// put subnodes into node
  
while($nodes = array_pop($nodes[1])) {

      
$nodecontent = array();

      
// Content1
      
preg_match("#<content1>(.*?)</content1>#",$eventreihe,$val);
      
$nodecontent['content1'] = $val[1];
      
      
$allnodes[] = $nodecontent;
      
   }
   return
$allnodes;
}

?>
Chris Hester
28-Jul-2005 08:06
The array generated from XML stores not only the elements but also any spaces and linebreaks between the tags. This results in a much longer array. (I had 24 array fields instead of 10!) To cure this use the following code when creating the parser:

<?php
$xml_parser
= xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
?>
grusin at gmail dot com
20-Jul-2005 02:54
Here is a simple patch to peter's xml2array function.

Replace:

<?
case 'open':
$tag_or_id = (array_key_exists ('attributes', xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
$ptrs[$level][$tag_or_id] = array ();
$ptrs[$level+1] = & $ptrs[$level][$tag_or_id];
break;
?>

with:

<?
case 'open':
$tag_or_id = (array_key_exists ('attributes', $xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
$ptrs[$level][$tag_or_id][] = array ();
$ptrs[$level+1] = & $ptrs[$level][$tag_or_id][count($ptrs[$level][$tag_or_id])-1];
break;
?>

and now code should handle multiple element case :)
Dustin
13-Jul-2005 11:39
If you happen to have problems with weird charaters, I added this code to the first line of dUDA's function:

$XML = utf8_decode($XML);
PhF at madmac dot fr
24-Jun-2005 04:11
The code previously posted by noob at noobsrule dot com doesn't work when the same tag name is used at different levels.
(but perhaps "$php_used_prefix" was intended for that ?)
For example:
<RatedShipment>
  <TransportationCharges>
   ...
  </TransportationCharges>
  <RatedPackage>
   <TransportationCharges>
     ...
   </TransportationCharges>
  </RatedPackage>
</RatedShipment>
<?php
   $p
= xml_parser_create();
  
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
  
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
  
xml_parse_into_struct($p, $UPSxml, $values, $idx);
  
xml_parser_free($p);
  
  
// tracking used keys
  
$usedKeys = array();
  
$deepLevel = -1;

  
// start a php array string (evaluated later)
  
$forEvalPrefix = '$xml_array';
  
  
// loop throught the value array
  
foreach ($values as $key => $val) {
      
$tagName = $val['tag']; // pass the key tag into a more friendly looking variable
      
$level = $val['level']; // idem
      
if($val['type'] == 'open') {
          
$deepLevel++; // increase deep level
          
$forEvalPrefix .= '[\''. $tagName .'\']';
          
          
// begin used keys checks to allow multidimensionatity under the same tag
          
(isset($usedKeys[$level][$tagName])) ? $usedKeys[$level][$tagName]++ : $usedKeys[$level][$tagName] = 0;
          
$forEvalPrefix .= '['. $usedKeys[$level][$tagName] .']';
       }
       if(
$val['type'] == 'complete') {
           (
$level > $deepLevel) ? $deepLevel++ : ''; // increase $deepLevel only if current level is bigger
          
$tagValue = addslashes($val['value'] ); // format the value for evaluation as a string
          
$forEvalSuffix = '[\''. $tagName .'\'] = \''. $tagValue .'\';'; // create a string to append to the current prefix
          
$forEval = $forEvalPrefix . $forEvalSuffix; // (without "$php_used_prefix"...)
          
eval($forEval); // write the string to the array structure
      
}
       if(
$val['type'] == 'close') {
           unset(
$usedKeys[$deepLevel]); // Suppress tagname's keys useless
          
$deepLevel--;
          
$forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off the used keys node
          
$forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); // cut off the end level of the array string prefix
      
}
   }
echo
'<pre>';
print_r ($xml_array);
echo
'</pre>';
?>

MADmac ;-)
peter at elemental dot org
15-Jun-2005 11:46
Quick addendum to my post below... The code does *not* handle cases where multiple elements are used without attribute IDs, as is the case with the moldb example.  (It is probably worth noting that some of the other code below doesn't do this either, and is still more complex.)

It is not difficult to add that functionality, but I have yet to find an elegant way to integrate that feature, and since I don't need that functionality at the moment, I'm going leave that as an exercise to the reader.  ;-)

-p.
peter at elemental dot org
15-Jun-2005 07:09
Some of the code posted previously seemed absurdly complex, so I thought I'd see if I could simplify things a bit. Here's what I came up with.  (I'm still a PHP newbie, so perhaps I simplified too much.  ;-)  Untested for speed, but it *should* be pretty quick since it uses references instead of lookups or (gack) evals.

Enjoy!

/*
 *  Convert a string containing XML into a nested array
 *
 *  Takes a raw XML string as input
 *  Returns a nested array
 */

function xml2array ($xml_data)
{
   // parse the XML datastring
   $xml_parser = xml_parser_create ();
   xml_parse_into_struct ($xml_parser, $xml_data, $vals, $index);
   xml_parser_free ($xml_parser);

   // convert the parsed data into a PHP datatype
   $params = array();
   $ptrs[0] = & $params; 
   foreach ($vals as $xml_elem) {
       $level = $xml_elem['level'] - 1;
       switch ($xml_elem['type']) {
       case 'open':
           $tag_or_id = (array_key_exists ('attributes', $xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
           $ptrs[$level][$tag_or_id] = array ();
           $ptrs[$level+1] = & $ptrs[$level][$tag_or_id];
           break;
       case 'complete':
           $ptrs[$level][$xml_elem['tag']] = (isset ($xml_elem['value'])) ? $xml_elem['value'] : '';
           break;
       }
   }
  
   return ($params);

}
gleber at mapnet dot pl
27-Mar-2005 07:03
XMLtoArray created by dudus at onet dot pl was not good enough for me, that's why i decided to create my own version, and here's the result:

<?php

function &last(&$array) {
if (!
count($array)) return null;
end($array);
return
$array[key($array)];
}

function
myParseXML(&$vals, &$dom, &$lev) {
   do {
      
$curr = current($vals);
      
$lev = $curr['level'];
       switch (
$curr['type']) {
           case
'open':
               if (isset(
$dom[$curr['tag']])) {
                  
$tmp = $dom[$curr['tag']];
                   if (!
$tmp['__multi'])
                      
$dom[$curr['tag']] = array('__multi' => true, $tmp);
                  
array_push($dom[$curr['tag']], array());
                  
$new =& last($dom[$curr['tag']]);
               } else {
                  
$dom[$curr['tag']] = array();
                  
$new =& $dom[$curr['tag']];
               }
              
next($vals);
              
myParseXML(&$vals, $new, $lev);
               break;
           case
'cdata':
               break;
           case
'complete':
               if (!isset(
$dom[$curr['tag']]))
                  
$dom[$curr['tag']] = $curr['value'];
               else {
                   if (
is_array($dom[$curr['tag']]))
                      
array_push($dom[$curr['tag']] , $curr['value']);
                   else
                      
array_push($dom[$curr['tag']] = array($dom[$curr['tag']]) , $curr['value']);
               }
               break;
           case
'close':
               return;
       }
   }
   while (
next($vals)!==FALSE);
}

function
MyXMLtoArray($XML) {
      
$xml_parser = xml_parser_create();
      
xml_parse_into_struct($xml_parser, $XML, $vals);
      
xml_parser_free($xml_parser);
      
reset($vals);
      
$dom = array(); $lev = 0;
      
myParseXML($vals, $dom, $lev);
       return
$dom;
}

?>

MyXMLtoArray() is that function you should call, other are for "internal" use.

P.S. It's 4-5 time faster then dudus at onet dot pl 's code
noob at noobsrule dot com
25-Jan-2005 02:59
This is my first time contributing but I found all the examples either didn't work or were beyond my comprehension.  I also wanted to maintain the XML multi-dimensionality so I could do loops based on keys I was expecting rather than numbers or some other whatnot.  This script is probably way inefficient, but should give you a multidimensional array with a depth that will match the XML source.  Great for situations when you aren't sure what you'll be getting back.  Its also only based on the values array of xml_parse_into_str() using the 3 tag types (open, complete, and close) -- hopefully it will make more sense to other noobs.

Basically it just constructs a string ($php_prefix) that is tweaked if it hits an "open" or "close" tag and evaluated if it hits a "complete" tag.

Thanks.

<?php

# initiate curl session...
$ch = curl_init( "http://yourtargethost" ) or die ( "could not initialize curl" );

# set curl options...
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $request_str );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 );
curl_setopt( $ch, CURLOPT_REFERER, https://foobar.com" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );

# execute curl commands...
$curl_result = curl_exec( $ch ) or die ( "could not execute the post" );
curl_close( $ch ); // close curl session

# pass the XML result into 2 arrays ( index and values )...
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $curl_result, $vals, $index);
xml_parser_free($p);

# create an empty used key tracker...
$used_keys = array();

# start a php array string (evaluated later)...
$php_prefix = "$"."my_array";

# loop through the values array...
foreach ($vals as $key => $val) {
   if(
$val['type'] == "open" ) {
      
$my_tag = $val['tag']; // pass the key tag into a more friendly looking variable...
      
$php_prefix .= "['".$my_tag."']"; # add a new level to the array string...

       # begin used key checks to allow multidimensionality under the same tag, adds a number to the prefix if necessary...
      
if( ! array_key_exists( $my_tag, $used_keys ) ) {
          
$used_keys[$my_tag] = 0;
          
$php_prefix .= "[".$used_keys[$my_tag]."]";
       }else {
          
$used_keys[$my_tag]++; // increment the key tracker to avoid overwriting existing key...
          
$php_prefix .= "[".$used_keys[$my_tag]."]";
       }
   } elseif(
$val['type'] == "complete" ) {
      
$my_value = addslashes( $val['value'] ); # format the value for evaluation as a string...
      
$php_suffix = "['".$val['tag']."'] = \"".$my_value."\";"; # create a string with the tag and value to append to the current prefix (just used here)...
      
$php_str = $php_prefix.$php_used_prefix.$php_suffix;
       eval(
$php_str ); # write the string to the array structure...
  
}
   if(
$val['type'] == "close" ) {
      
$php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); # cut off the used keys node...
      
$php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); # cut off the end level of the array string prefix...
  
}
}

echo
"<pre>";
//print_r ($index);
//echo "<br><br><br>";
//print_r ($vals);
//echo "<br><br><br>";
print_r ($my_array);
echo
"</pre>";

?>
dudus at onet dot pl
18-Jan-2005 10:33
This function return array from XML. Included attributes and tags in same level.
I wrote this function based on mmustafa code.

enjoy!
dUDA

-------------------------------

<?
function XMLtoArray($XML)
{
  
$xml_parser = xml_parser_create();
  
xml_parse_into_struct($xml_parser, $XML, $vals);
  
xml_parser_free($xml_parser);
  
// wyznaczamy tablice z powtarzajacymi sie tagami na tym samym poziomie
  
$_tmp='';
   foreach (
$vals as $xml_elem)
   {
      
$x_tag=$xml_elem['tag'];
      
$x_level=$xml_elem['level'];
      
$x_type=$xml_elem['type'];
       if (
$x_level!=1 && $x_type == 'close')
       {
           if (isset(
$multi_key[$x_tag][$x_level]))
              
$multi_key[$x_tag][$x_level]=1;
           else
              
$multi_key[$x_tag][$x_level]=0;
       }
       if (
$x_level!=1 && $x_type == 'complete')
       {
           if (
$_tmp==$x_tag)
              
$multi_key[$x_tag][$x_level]=1;
          
$_tmp=$x_tag;
       }
   }
  
// jedziemy po tablicy
  
foreach ($vals as $xml_elem)
   {
      
$x_tag=$xml_elem['tag'];
      
$x_level=$xml_elem['level'];
      
$x_type=$xml_elem['type'];
       if (
$x_type == 'open')
          
$level[$x_level] = $x_tag;
      
$start_level = 1;
      
$php_stmt = '$xml_array';
       if (
$x_type=='close' && $x_level!=1)
          
$multi_key[$x_tag][$x_level]++;
       while(
$start_level < $x_level)
       {
            
$php_stmt .= '[$level['.$start_level.']]';
             if (isset(
$multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
                
$php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
            
$start_level++;
       }
      
$add='';
       if (isset(
$multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type=='open' || $x_type=='complete'))
       {
           if (!isset(
$multi_key2[$x_tag][$x_level]))
              
$multi_key2[$x_tag][$x_level]=0;
           else
              
$multi_key2[$x_tag][$x_level]++;
            
$add='['.$multi_key2[$x_tag][$x_level].']';
       }
       if (isset(
$xml_elem['value']) && trim($xml_elem['value'])!='' && !array_key_exists('attributes',$xml_elem))
       {
           if (
$x_type == 'open')
              
$php_stmt_main=$php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
           else
              
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
           eval(
$php_stmt_main);
       }
       if (
array_key_exists('attributes',$xml_elem))
       {
           if (isset(
$xml_elem['value']))
           {
              
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
               eval(
$php_stmt_main);
           }
           foreach (
$xml_elem['attributes'] as $key=>$value)
           {
              
$php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
               eval(
$php_stmt_att);
           }
       }
   }
     return
$xml_array;
}   
// END XMLtoArray
?>
memandeemail at gmail dot com
09-Dec-2004 11:09
a BREAK on right place, will be faster and bugprevent

   /**
   * @return bool
   * @param xml_array $values
   * @param xml_array[reference] $tags
   * @desc Reconstruct xml_parse_into_struct to open and close concept
   */
   function reconstruct_tag($values, &$tags) {
       unset($return);
       foreach ($tags as $tkey => $tvalue) {
           foreach ($tvalue as $vvalue) {
               if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') {
                   $return[$tkey][] = $vvalue;
               }
               if ($values[$vvalue]['type'] == 'open') {
                   foreach ($tvalue as $tv_value) {
                       if ($values[$tv_value]['type'] == 'close'
                       and $values[$tv_value]['level'] == $values[$vvalue]['level']
                       and $tv_value > $vvalue) {
                           $return[$tkey][] = $tv_value;
                           break;
                       }
                   }
               }
           }
       }
       $tags = $return;
       return (bool) $tags;
   }
memandeemail at gmail dot com
03-Dec-2004 08:32
If you have de XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<teste>
   <teste>
       <teste>
       </teste>
   </teste>
</teste>

this function will return this tags:
Array
(
   [teste] => Array
       (
           [0] => 0
           [1] => 1
           [2] => 2
           [3] => 3
           [4] => 4
       )

)

With this function (right below) will convert the struc to OPEN and CLOSE concept.

The New Struct:
Array
(
   [teste] => Array
       (
           [0] => 0
           [1] => 4
           [2] => 1
           [3] => 3
           [4] => 2
       )

)
The Function:
/**
 * @return bool
 * @param xml_array $values
 * @param xml_array[reference] $tags
 * @desc Reconstruct xml_parse_into_struct
*/
function reconstruct_tag($values, &$tags) {
   unset($return);
   foreach ($tags as $tkey => $tvalue) {
       foreach ($tvalue as $vvalue) {
           if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') {
               $return[$tkey][] = $vvalue;
           }
           if ($values[$vvalue]['type'] == 'open') {
               foreach ($tvalue as $tv_value) {
                   if ($values[$tv_value]['type'] == 'close'
                   and $values[$tv_value]['level'] == $values[$vvalue]['level']) {
                       $return[$tkey][] = $tv_value;
                   }
               }
           }
       }
   }
   $tags = $return;
   return (bool) $tags;
}

Ahh, DO NOT forget this comment:

jukkaho at mail dot student dot oulu dot fi
29-Jun-2004 12:17
Everyone seems to forget the return value of this function (including people making examples!), but it is there for a reason:
0 if failed (invalid XML), 1 if succeeded.

Return value really is an integer, be careful with those === operators.
rob at digital hyphen crocus dot com
04-Sep-2004 12:21
I wrote this function in a bad mood, to get some data you can loop through. sprint_r or var_dump its output to see what it does. It's not pretty, but at least you can loop through the data without a LOT of hassle.

This can also be done with handlers, but I couldn't be bothered with global variables.

<?php

$data
= xml_parse_into_struct($somexml);

function
xml_getmeaningfuldata($data) {
foreach(
$data as $element) {
       if (
$element['type'] == "open") {
               if (!
$elementnos[$parents.$element['tag']]) {
                      
$elementnos[$parents.$element['tag']] = 1;
               }
              
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['attributes'] = $element['attributes'];
              
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['value'] = trim($element['value']);
              
$parents .= $element['tag'].":".$elementnos[$parents.$element['tag']]."^";
       }
       if (
$element['type'] == "cdata") {
               if (
trim($element['value'])) {
                      
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['values'] = $element['value'];
               }
       }
       if (
$element['type'] == "complete") {
               if (!
$elementnos[$parents.$element['tag']]) {
                      
$elementnos[$parents.$element['tag']] = 1;
               }
              
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['attributes'] = $element['attributes'];
              
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['value'] = trim($element['value']);
              
$elementnos[$parents.$element['tag']]++;
       }
       if (
$element['type'] == "close") {
              
$parents = substr($parents, 0, strrpos(substr($parents, 0, -1), "^")+1);
              
$elementnos[$parents.$element['tag']]++;
//              if (sizeof($parents) == 1) unset($parents);
      
}
}

return
$elements;

}
happyseb at free dot fr
24-Aug-2004 11:51
I would just add something about the solution proposed by "gordon at kanazawa dot ac dot jp".

Something went wrong about the fact that "cdata" are treated the same way as "complete" in the xml_get_children first and in the xml_get_child then.

When ?
with this method, something like this :
<tagA>
  <tagB>valueB</tagB>
  valueA
  <tagC>valueC</tagC>
</tagA>
is considered as the same thing as
<tagA>
  <tagB>valueB</tagB>
  <tagA>valueA</tagA>
  <tagC>valueC</tagC>
</tagA>

The algorithm is nomore bijective.

Even if it might not be perfect :-) i propose a little modification of the xml_get_child function :

function xml_get_child(&$v, $children=NULL) {
     $c = array(); // the "child"
     if (isset($v['tag']) && $v['type'] != 'cdata') $c['tag'] = $v['tag'];
     if (isset($v['value'])) $c['value'] = $v['value'];
     if (isset($v['attributes'])) $c['attributes'] = $v['attributes'];
     if (is_array($children)) $c['children'] = $children;
     return $c;
}

moreover, the following function will take the tree (made from the xml_get_tree function) as a parameter and build the xml content back

   /*
   // setTree
   // set xml Tree from a well-formed array
   // manage indentation
   // Parameters are :
   // $1 (needed) : Tree to transform
   // $2 (optional) : Indentation type (default is one tabulation)
   // $3 (optional) : First indent (default is empty string)
   */

   function setTree($Atree, $indentType = '    ', $Sindent = '')
   {
     if (! isset ($Atree['tag']) && isset ($Atree['value'])) return ($Sindent.trim($Atree['value'])."\r\n") ;
     else {
       $Stemp = $Sindent.'<'.$Atree['tag'].' ' ;
       if (isset ($Atree['attributes']) && is_array ($Atree['attributes'])) {
         foreach ($Atree['attributes'] as $Aattribute => $Svalue) $Stemp .= $Aattribute.'="'.$Svalue.'" ' ;
       }
       if (! isset ($Atree['children']) && ! isset ($Atree['value'])) $Stemp .= '/>'."\r\n" ;
       else {
         $Stemp .= '>'."\r\n" ;
         if (isset ($Atree['value'])) $Stemp .= $Sindent.$indentType.trim($Atree['value'])."\r\n" ;
         if (isset ($Atree['children']) && is_array ($Atree['children']))
           foreach ($Atree['children'] as $Achild) $Stemp .= $this->setTree($Achild, $indentType, $Sindent.$indentType) ;
         $Stemp .= $Sindent.'</'.$Atree['tag'].'>'."\r\n" ;
       }
       return ($Stemp) ;
     }
   }
sdh33 at cornell dot edu
14-Jun-2004 01:12
(The previous post I wrote was incorrect.  I fix that herein)
I used the algorithm from stephane for writing back to a file, but ran into the following problem.  My original XML looks like

<ENTRY id="1"> ... </ENTRY><ENTRY id="2"> ... </ENTRY>

This got saved back as

<1> ... </1><2> ... </2>,

which expat doesn't like.  So I propose the following modification:

function writeXmlToFile($data, $f, $niv = 0) {
  foreach($data as $name => $elem) {
   if (preg_match("/\A[0-9]/",$name)) {
     $open = "ENTRY id=\"$name\""; $name = "ENTRY";
   } else {
     $open = $name;
   }
   if(is_array($elem)) {
     fwrite($f, str_repeat("  ", $niv)."<".$open.">"."\n");
     writeXmlToFile($elem, $f, $niv + 1);
     fwrite($f, str_repeat("  ", $niv)."</".$name.">"."\n");
   } else {
     fwrite($f, str_repeat("  ", $niv)."<".$open.">".$elem."</".$name.">"."\n");
   }
  }
}
stephane dot dekeyzer at noemail dot com
09-Apr-2004 02:54
for writing the array of mmustafa back to a xml file, use this code:

function writeXmlFile($fileName, $struct){
  $f = fopen($fileName, "w");
  writeXmlToFile($struct, $f);
  fclose($f);
}

function writeXmlToFile($data, $f, $niv = 0){
  foreach($data as $name => $elem){
   if(is_array($elem)){
     fwrite($f, str_repeat("  ", $niv)."<".$name.">"."\n");
     writeXmlToFile($elem, $f, $niv + 1);
     fwrite($f, str_repeat("  ", $niv)."</".$name.">"."\n");
   }
   else{
     fwrite($f, str_repeat("  ", $niv)."<".$name.">".$elem."</".$name.">"."\n");
   }
  }
}
gordon at kanazawa dot ac dot jp
26-Jan-2004 12:36
Many thanks to "dave at ovumdesign dot com" for his excellent routines based on the fine work of "gdemartini at bol dot com". I offer a further refinement below, which removes the duplication of the code to create a child node. The resulting tree is the same as in Dave's earlier version and so can be compacted using his "xml_condense_tree"

function xml_get_child(&$v, $children=NULL) {
   $c = array(); // the "child"
   if (isset($v['tag'])) $c['tag'] = $v['tag'];
   if (isset($v['value'])) $c['value'] = $v['value'];
   if (isset($v['attributes'])) $c['attributes'] = $v['attributes'];
   if (is_array($children)) $c['children'] = $children;
   return $c;
}
function xml_get_children(&$values, &$i) {
   $children = array();
   while ($i < count($values)) {
     $v = &$values[$i++];
     switch ($v['type']) {
         case 'cdata':
         case 'complete':
           $children[] = xml_get_child($v);
           break;
         case 'open':
           $children[] = xml_get_child($v, xml_get_children($values, $i));
           break;
         case 'close':
           break 2; // leave "while" loop
     }
   }
   return $children;
}
function xml_get_tree($file) {
   $data = join('', file($file));

   $p = xml_parser_create();
   xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
   xml_parser_set_option($p, XML_OPTION_SKIP_WHITE,  1);
   xml_parse_into_struct($p, $data, $values);
   xml_parser_free($p);

   return xml_get_children($values, $i=0);
}
mmustafa at vsnl dot com
06-Jan-2004 04:19
A small and simple php code to convert the raw array returned by xml_parse_into_struct into a proper data structure ..

<?php
$file
= "data.xml";
$xml_parser = xml_parser_create();

if (!(
$fp = fopen($file, "r"))) {
   die(
"could not open XML input");
}

$data = fread($fp, filesize($file));
fclose($fp);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

$params = array();
$level = array();
foreach (
$vals as $xml_elem) {
  if (
$xml_elem['type'] == 'open') {
   if (
array_key_exists('attributes',$xml_elem)) {
     list(
$level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
   } else {
    
$level[$xml_elem['level']] = $xml_elem['tag'];
   }
  }
  if (
$xml_elem['type'] == 'complete') {
  
$start_level = 1;
  
$php_stmt = '$params';
   while(
$start_level < $xml_elem['level']) {
    
$php_stmt .= '[$level['.$start_level.']]';
    
$start_level++;
   }
  
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
   eval(
$php_stmt);
  }
}

echo
"<pre>";
print_r ($params);
echo
"</pre>";
?>

Converts an XML document like ..

<country id="ZZ">
<name>My Land</name>
<location>15E</location>
<area>40000</area>
<state1>
<name>Hi State</name>
<area>1000</area>
<population>2000</population>
<city1>
<location>13E</location>
<population>500</population>
<area>500</area>
</city1>
<city2>
<location>13E</location>
<population>500</population>
<area>5000</area>
</city2>
</state1>
<state2>
<name>Low State</name>
<area>3000</area>
<population>20000</population>
<city1>
<location>15E</location>
<population>5000</population>
<area>1500</area>
</city1>
</state2>
</country>

To a php datastructure like ..

Array
(
   [ZZ] => Array
       (
           [NAME] => My Land
           [LOCATION] => 15E
           [AREA] => 40000
           [STATE1] => Array
               (
                   [NAME] => Hi State
                   [AREA] => 1000
                   [POPULATION] => 2000
                   [CITY1] => Array
                       (
                           [LOCATION] => 13E
                           [POPULATION] => 500
                           [AREA] => 500
                       )

                   [CITY2] => Array
                       (
                           [LOCATION] => 13E
                           [POPULATION] => 500
                           [AREA] => 5000
                       )

               )

           [STATE2] => Array
               (
                   [NAME] => Low State
                   [AREA] => 3000
                   [POPULATION] => 20000
                   [CITY1] => Array
                       (
                           [LOCATION] => 15E
                           [POPULATION] => 5000
                           [AREA] => 1500
                       )

               )

       )

)
26-Sep-2003 06:43
actually, using the notation

$object->property is the right one

and you can use it always, preventing errors with arrays
Eric
22-Sep-2003 11:50
Yet another XML -> nested array converter.

This is based on http://www.devdump.com/phpxml.php, but adds features while reducing code complexity.

It can take several forms of input:
 - string (buffer)
 - file handle (pipe)
 - file name -or-
 - URL

It can optionally collapse arrays of same-named tags to a single element if there is only one tag with a given name.

It can optionally preserve tag order for XML applications where position is significant, such as CallXML.

XMLParser.obj at:
http://eric.pollmann.net/work/public_domain/

Corrections and suggestions enthusiastically welcomed.  :)
Adam Tylmad
12-Sep-2003 08:01
I've just debugged and optimized my xmlparser.

gdemartini's xml-example generates this structure:

stdClass Object
(
   [FORM] => stdClass Object
       (
           [SECTION] => stdClass Object
               (
                   [NAME] => Data
                   [EDITFIELD] => stdClass Object
                       (
                           [LABEL] => Text:
                           [NAME] => poll_text
                           [LENGTH] => 255
                           [SIZE] => 56
                           [REQUIRED] => T
                       )

                   [MEMOFIELD] => stdClass Object
                       (
                           [LABEL] => Options
                           [NAME] => options
                           [COLS] => 56
                           [ROWS] => 5
                           [REQUIRED] => T
                       )
               )
       )
)

the moldb example generates this structure:

stdClass Object
(
   [MOLDB] => stdClass Object
       (
           [MOLECULE] => Array
               (
                   [0] => stdClass Object
                       (
                           [NAME] => Alanine
                           [SYMBOL] => ala
                           [CODE] => A
                           [TYPE] => hydrophobic
                       )

                   [1] => stdClass Object
                       (
                           [NAME] => Lysine
                           [SYMBOL] => lys
                           [CODE] => K
                           [TYPE] => charged
                       )
               )
       )
)

here is the code. enjoy!

<?php

class XMLParser {
   var
$path;
   var
$result;

   function
cleanString($string) {
       return
trim(str_replace("'", "&#39;", $string));
   }
  
   function
XMLParser($encoding, $data) {
      
$this->path = "\$this->result";
      
$this->index = 0;
      
      
$xml_parser = xml_parser_create($encoding);
      
xml_set_object($xml_parser, &$this);
      
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
      
xml_set_character_data_handler($xml_parser, 'characterData');

      
xml_parse($xml_parser, $data, true);
      
xml_parser_free($xml_parser);
   }
  
   function
startElement($parser, $tag, $attributeList) {
      
$this->path .= "->".$tag;
       eval(
"\$data = ".$this->path.";");
       if (
is_array($data)) {
          
$index = sizeof($data);
          
$this->path .= "[".$index."]";
       } else if (
is_object($data)) {
           eval(
$this->path." = array(".$this->path.");");
          
$this->path .= "[1]";
       }

       foreach(
$attributeList as $name => $value)
           eval(
$this->path."->".$name. " = '".XMLParser::cleanString($value)."';");
   }
  
   function
endElement($parser, $tag) {
      
$this->path = substr($this->path, 0, strrpos($this->path, "->"));
   }
  
   function
characterData($parser, $data) {
       if (
$data = XMLParser::cleanString($data))
           eval(
$this->path." = '$data';");
   }
}

?>
plusplus7 at hotmail dot com
18-Jun-2003 09:40
Quick and dirty function to get original file from $vals array in manual example. Build on this for custom parsing of individual elements (something like this should be built-in IMO).

<pre>
function parseVals($vals){
   foreach($vals as $valobj){
       if($valobj['type']=='open' || $valobj['type']=='complete'){
           $retstr .= '<'.$valobj['tag'];
           if(is_array($valobj['attributes'])){
               foreach($valobj['attributes'] as $key=>$val){
                   $retstr .= " $key=\"$val\"";
               }
           }
       }
       if($valobj['type']=='open'){
           $retstr .= '>'.$valobj['value'];
       }elseif($valobj['type']=='complete' && $valobj['value']){
           $retstr .= '>'.$valobj['value'].'</'.$valobj['tag'].'>';
       }elseif($valobj['type']=='complete'){
           $retstr .= ' />';
       }elseif($valobj['type']=='cdata'){
           $retstr .= $valobj['value'];
       }elseif($valobj['type']=='close'){
           $retstr .= '</'.$valobj['tag'].'>';
       }

   }
   return $retstr;
}

</pre>
masuod_a at hotmail dot com
24-Nov-2002 08:32
wanna load object from xml file? this function take a tree returned by xml_get_tree() [dave@ovumdesign.com's version] and return an object.
it can't work with attributes, i hate attributes (except id), but may be useful for you.

- how to use :
------------------------- xml file -------------
<config>
   <images>
     <image id="1">
       <name>my pet</name>
       <file>pet001.jpg</file>
     </image>
     <image id="2">
       <name>harry's pet</name>
       <file>pet003.jpg</file>
     </image>
   </images>
   <library>lib_functions.php</library>
</config>
----------------------------------------------
$config=xml_to_object($xml_tree);
$config->library;    #return "lib_functions.php"
$config->images->image__1->name; #return "my pet"
----------------------------------------------
function xml_to_object($xml_tree,$root=false){
             $object_name=$xml_tree['tag'];
             foreach ($xml_tree['children'] as $children){
                         $child_id=$children['attributes']['id'];
                         $child_name=$children['tag'];
                         $child_name.=($child_id) ? "__".$child_id:'';

                         if (is_array($children['children'])) {   
                                     $child_object=xml_to_object($children);
                                     $temp_object->$child_name=$child_object;
                         } else {
                                   $temp_object->$child_name=$children['value'];
                         }
             }

             if (!$root)
                         $xml_object=$temp_object;
             else      $xml_object->$object_name=$temp_object;

             return $xml_object;
}

also you can use this function for loading array from xml , but i suggest you to use kris@h3x.com's functions[ http://www.devdump.com/phpxml.php ].

function xml_to_array($xml_tree,$root=false){
   $array_name=$xml_tree['tag'];
   foreach ($xml_tree['children'] as $children){
       $child_id=$children['attributes']['id'];
       $child_name=$children['tag'];
       $child_name.=($child_id) ? "__".$child_id:'';
       if (is_array($children['children'])) {
           $child_array=xml_to_array($children);
           $temp_array[$child_name]=$child_array;
       } else {           
           $temp_array[$child_name]=$children['value'];
       }
   }
  
   if (!$root)
     $xml_array=$temp_array;
   else
     $xml_array[$array_name]=$temp_array;
  
   return $xml_array;
}
bisqwit at iki dot fi dot not dot for dot ads dot invalid
21-Nov-2002 12:33
I made my own xml2tree()-function.
The difference to the previously suggested ones on this manual page is that this creates a tree where the tags are keys.

Example input:

<tree>
 <leaf name="gruu"> pl </leaf>
 <animal type="cat" />
</tree>

Example output:

Array
( 'tree' => Array
     ( 0 => Array
         ( 'children' => Array
               ( 'leaf' => Array
                   ( 0 => Array
                         ( 'attrs' => Array('name' => 'gruu'),
                           'values' => Array(0 => ' pl ')
                   )    ),
                 'animal' => Array
                   ( 0 => Array
                         ( 'attrs' => Array('type' => 'cat')
)    )  )    )    )  )

function xml2tree2($string)
{
  $parser = xml_parser_create();
  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, $string, $vals, $index);
  xml_parser_free($parser);
 
  $stack = array( array() );
  $stacktop = 0;
  $parent = array();
  foreach($vals as $val)
  {
   $type = $val['type'];
   if($type=='open' || $type=='complete')
   {
     // open tag
     $stack[$stacktop++] = $tagi;
     $tagi = array('tag' => $val['tag']);

     if(isset($val['attributes']))  $tagi['attrs'] = $val['attributes'];     
     if(isset($val['value']))
       $tagi['values'][] = $val['value'];
   }
   if($type=='complete' || $type=='close')
   {
     // finish tag
     $tags[] = $oldtagi = $tagi;
     $tagi = $stack[--$stacktop];
     $oldtag = $oldtagi['tag']; 
     unset($oldtagi['tag']);
     $tagi['children'][$oldtag][] = $oldtagi;
     $parent = $tagi;
   }
   if($type=='cdata')
   {
     $tagi['values'][] = $val['value'];
   }
  }
  return $parent['children'];
}       

Ps: Note that the xml parsing for some reason chokes if the values contain <> (even inside ""). You need to escape them as &lt; and &gt;.
Same for & (for obvious reasons). Escape it as &amp;.
For example "&nbsp;" should be written as "&amp;nbsp;".
joeldegan at yahoo dot com
20-Nov-2002 08:40
Using one of the nice function above, I was able to get these two great structures below on nmap info for a machine and then the PID lists for each open port.. Very handy for my current project.

$test = `nmap -sU -sS localhost -oX -`;
$ret = xml2array($test);
$z=0;
while (list($key,$val)=each($ret["host"][0][3]["children"])){
   if ($val["tag"] == "PORT"){
       $new[$z]["PROTOCOL"] = $val["attributes"]["PROTOCOL"];
       $new[$z]["PORT"] = $val["attributes"]["PORTID"];
      
       //echo $val["attributes"]["PROTOCOL"]."\n";   
       //echo $val["attributes"]["PORTID"]."\n";
       $z++;
   }//fi
}//wend

print_r($new);

function port_pids($portnum){
   $ret = `lsof -i :$portnum -F pc`;
   $ret = explode("\n", $ret);
   $z=0;
   for($a=0;$a<count($ret);$a++){
       $port = $ret[$a];
       $a++;
       $com = $ret[$a];
       $new[$z]["PID"]=substr($port, 1);
       $new[$z]["COMMAND"]=substr($com, 1);
       $z++;
   }//wend
   return $new;
}//end function

print_r(port_pids("3306"));
no at chance dot com
09-Nov-2002 02:38
Thanks to gdemartini's tree algorithm , I was able to create a variation that display's the information in a way that allows you to then easily parse the information with less code and process time. Here's the variation and I've also posted a quick function at the bottom that allows you to visually display complex arrays in human readable format. Hope this helps someone as much as gdemartini's functions helped me.

I decided to post it on my server as opposed to the forums as it was too large and needed some good html formatting.  It can found at http://www.devdump.com/phpxml.php .
ambi at gmx dot de
06-Oct-2002 06:59
Sorry.. unreachable and unneeded code in my posting above :-)
Here is the fixed version:
function xml_condense_tree($tree) {
   if ($tree["children"]) {
           unset($nodecounters);
           foreach ($tree['children'] as $index => $node) {
               if (!isset($nodecounters[$node['tag']]))
               $nodecounters[$node['tag']]=1;
               else $nodecounters[$node['tag']]++;
           }
       foreach ($tree['children'] as $index => $node) {
           if (isset($node['children'])) {
               $tree['children'][$index] = xml_condense_tree($node);
           } elseif (isset($node['value']) and !$node['attributes'] and $nodecounters[$node['tag']]==1) {
               $tree['values'][$node['tag']] = $node['value'];
               unset($tree['children'][$index]);
           }
       }
   }
   if (!$tree['children']) unset($tree['children']);
  
   return $tree;
}
09-Aug-2002 04:20
This is based on a comment above by mh@mainlab.de.  This version makes more sense to me and I included a few comments that might make it more usefull for specific purposes.

// Look through $vals and $index,
// generated by parse_xml_data(),
// for $location.  $location resembles a unix path.
// E.g. "my/xml/path/to/my/data".
// Note: This is NOT a full-feature xml utility.
// Your path must be unique in the xml data
// and the tags must not contain slashes.
//
// returns: cdata from last element of $location
//          or NULL.
function get_xml_values($vals, $index, $location) {
  $location = explode('/', $location);
  $location_level = count($location);
  for ($n=0; $n<$location_level; ++$n) {
   $val_indexes=$index[$location[$n]];
   $good=0;
   for ($i=0; $i<count($val_indexes); ++$i) {
     $v = $vals[$val_indexes[$i]];
     // level is 1 based, n is 0 based:
     if ($v['level']-1 == $n) {
       if ($v['type'] == 'open') {
         // We found an open tag at the correct level.
         $good=1;
         break; // Remove break to avoid 'open' hiding
                 // 'complete' at last $n. (Speed Vs. Robustness:
                 // For now, I'm going with speed).
       }
       if ($v['type'] == 'complete') {
         // We found it, after finding all the prior elements:
         // You may want to change this to
         // return $v
         // so you can call $v['value'] and $v['attributes']
         return $v['value'];
       }
     }
   }
   if (!$good) {
     return NULL;
   }
  }
}
farinspace-at-hotmail-dot-com
21-Jul-2002 09:41
Wow, took a while to consume all of this. I am a noobie at XML, in fact i just really started working with it today. I went ahead and added to the above code by gdemartini@bol.com.br. I also added the update by nyk@cowham.net.

Added 'value' => $vals[$i]['value'] to the open case, as some nodes have both cdata as well as a children node. In previous versions of the functions the cdata of a node(a) that also had children would get stored in the children's array of the parent node(a). Now only children values are in the children array and the cdata is in the value key of the parent node(a).

Added 'value' => $vals[$i]['value'] to tree array as the vals array produced by PHP includes a value to the top most node if no children exist (for completeness).

Also, using xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,1) will strip all white space from both around the XML tags as well as inside the cdata. I added a piece of code by waldo@wh-e.com to take care of this. All SPACE characters are now only stripped from around the XML tags and cdata spacing is retained.

CODE:
***************************************

function GetChildren($vals, &$i) {

   $children = array();

   while (++$i < sizeof($vals)) {

       // compair type
       switch ($vals[$i]['type']) {

           case 'cdata':
               $children[] = $vals[$i]['value'];
               break;
           case 'complete':
               $children[] = array(
                   'tag' => $vals[$i]['tag'],
                   'attributes' => $vals[$i]['attributes'],
                   'value' => $vals[$i]['value']
               );
               break;
           case 'open':
               $children[] = array(
                   'tag' => $vals[$i]['tag'],
                   'attributes' => $vals[$i]['attributes'],
                   'value' => $vals[$i]['value'],
                   'children' => GetChildren($vals, $i)
               );       
               break;
           case 'close':
               return $children;
       }
   }
}

function GetXMLTree($file) {

   $data = implode('', file($file));

   // by: waldo@wh-e.com - trim space around tags not within
   $data = eregi_replace(">"."[[:space:]]+"."<","><",$data);

   // XML functions
   $p = xml_parser_create();

   // by: anony@mous.com - meets XML 1.0 specification
   xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
   xml_parse_into_struct($p, $data, &$vals, &$index);
   xml_parser_free($p);

   $i = 0;
   $tree = array();
   $tree[] = array(
       'tag' => $vals[$i]['tag'],
       'attributes' => $vals[$i]['attributes'],
       'value' => $vals[$i]['value'],
       'children' => GetChildren($vals, $i)
   );

   return $tree;
}

***************************************
dave at ovumdesign dot com
03-Apr-2002 07:32
Here is another variation on gdemartini@bol.com.br's nice tree algorithm. I have included nyk@cowham.net's "cdata" suggestion. I made a few changes to suppress unitialized variable warnings with error_reporting(E_ALL), and made a few stylistic changes.

There are two functional modifications as well. The first is the added option to expat to turn off "case folding", since XML is technically a case-sensitive language. The second is the returned tree - instead of returning a single-element list (an array with index 0 pointing to the top node) like the original algorithm, it returns the top node itself. This saves you an unnecessary level of indirection.

function xml_get_children($vals, &$i) {
   $children = array();
   if (isset($vals[$i]['value'])) $children[] = $vals[$i]['value'];

   while (++$i < count($vals)) {
       switch ($vals[$i]['type']) {
       case 'cdata':
           $children[] = $vals[$i]['value'];
           break;

       case 'complete':
           $children[] = array(
               'tag'        => $vals[$i]['tag'],
               'attributes' => isset($vals[$i]['attributes'])?
                               $vals[$i]['attributes'] : null,
               'value'      => $vals[$i]['value'],
           );
           break;

       case 'open':
           $children[] = array(
               'tag'        => $vals[$i]['tag'],
               'attributes' => isset($vals[$i]['attributes'])?
                               $vals[$i]['attributes'] : null,
               'children'  => xml_get_children($vals, $i),
           );
           break;

       case 'close':
           return $children;
       }
   }
}

function xml_get_tree($file) {
   $data = join('', file($file));

   $parser = xml_parser_create();
   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, $data, $vals, $index);
   xml_parser_free($parser);

   return array(
       'tag'        => $vals[0]['tag'],
       'attributes' => isset($vals[0]['attributes'])?
                       $vals[0]['attributes'] : null,
       'children'  => xml_get_children($vals, $i = 0),
   );
}

This function takes a tree returned by xml_get_tree() and simplifies it similarly to voncken@mailandnews.com and oknapp@soly.de's suggestions. It finds any nodes that expat considered "complete" (childless) and contain no attributes, and it moves the tag name (as the key) and value into an element in the parent node's "values" array.

For example:
$tree = xml_get_tree('moldb.xml'); 
$tree = xml_condense_tree($tree);
echo $tree['children'][1]['values']['name'];

Outputs:
Lysine

function xml_condense_tree($tree) {
   foreach ($tree['children'] as $index => $node) {
       if (isset($node['children'])) {
           $tree['children'][$index] = xml_condense_tree($node);

       } elseif (isset($node['value']) and !$node['attributes']) {
           $tree['values'][$node['tag']] = $node['value'];
           unset($tree['children'][$index]);
       }
   }
    
   if (!$tree['children']) unset($tree['children']);
   return $tree;
}

Enjoy,
Dave
voncken at mailandnews dot com
06-Jan-2002 09:24
I needed something a bit different:
this is the result of my xml2array function (from the moldb example)

Array
(
   [molecule] => Array
       (
           [0] => Array
               (
                   [name] => Alanine
                   [symbol] => ala
                   [code] => A
                   [type] => hydrophobic
               )

           [1] => Array
               (
                   [name] => Lysine
                   [symbol] => lys
                   [code] => K
                   [type] => charged
               )

       )

)

function xml2array($data)
  {
   //mvo voncken@mailandnews.com
   //original ripped from  on the php-manual:gdemartini@bol.com.br   
   //to be used for data retrieval(result-structure is Data oriented)   
   $p = xml_parser_create();
   xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
   xml_parse_into_struct($p, $data, &$vals, &$index);
   xml_parser_free($p);

   $tree = array();
   $i = 0;
   //array_push($tree, array('tag' => $vals[$i]['tag'], 'attributes'=> $vals[$i]['attributes'],                            'children' => ));
   $tree = GetChildren($vals, $i);
   return $tree;
  }

 function GetChildren($vals, &$i)
  {                 
   $children = array();
   if ($vals[$i]['value']) array_push($children, $vals[$i]['value']);
      
   $prevtag = "";
   while (++$i < count($vals)) // so pra nao botar while true ;-)
   {     
     switch ($vals[$i]['type'])
     {         
         case 'cdata':
           array_push($children, $vals[$i]['value']);
           break;
         case 'complete':                     
           $children[ strtolower($vals[$i]['tag']) ] = $vals[$i]['value'];           
           break;

         case 'open':                               
          
           //restartindex on unique tag-name
           $j++;
           if ($prevtag <> $vals[$i]['tag']) {
                   $j = 0;   
                   $prevtag = $vals[$i]['tag'];
           }                             
           $children[ strtolower($vals[$i]['tag']) ][$j] = GetChildren($vals,$i);
           break;

         case 'close':         
           return $children;
     }
   }   
  }