开发者

Parsing XML Document into Multidimensional Array

I am attempting to create a multidimensional array of unlimited depth from an XML file. I shall spare you the reasons why and just get straight to the point.

I would like the below code to parse an XML string, inserting all <item> and <nest> tags into an array. Any <item> tags that are found directly under a <nest> tag should be be inserted into an array contained inside the main array.

On the recurisve call I have passed the key to a new array as the second parameter, so I would expect it to begin adding items to this new array. This is not so, however. If anybody could assist me in fixing the problem, I would appreciate it.

The code should run with a simple C+P. Thank you.

class Parser
{
  static function subMenuRecursionArray($xml, $array = '', $itemcount = 1, $nestcount = 1)
  {
    foreach($xml->children() as $k => $v)
    {
        if ((string) $k == 'item')
        {
            $array["item$itemcount"]['text'] = (string) $v;
            $array["item$itemcount"]['command'] = (string) $v['command'];
            $itemcount++;
        }

        if ((string) $k == 'nest')
        {
            $ar开发者_开发知识库ray["nest$nestcount"] = array('name' => (string) $v['name'], 'items' => array());
            $nestcount++;

            self::subMenuRecursionArray($xml->nest, $array["nest".($nestcount-1)]['items'], $itemcount, $nestcount);
        }
    }
    return $array;
  }
}

$xml_fragment = '
<menu>
<item command="DefaultCommand">Main (Not nested 1)</item>
<item command="DefaultCommand">Main 2 (Not nested 2)</item>
<nest name="Cont">
    <item command="Contact">NESTED Contact 1</item>
    <item command="Contact">NESTED Contact 2</item>
  </nest>
</menu>';

$xml = simplexml_load_string($xml_fragment);
$array = Parser::subMenuRecursionArray($xml);
echo '<pre>' . print_r($array, 1) . '</pre>';


Change your code to this - notice that your call too subMenuRecursionArray is returning an array, but you aren't assigning that to anything - so it is just lost.

<?php
class Parser
{
  static function subMenuRecursionArray($xml, $array = '', $itemcount = 1, $nestcount = 1)
  {
    foreach($xml->children() as $k => $v)
    {
        if ((string) $k == 'item')
        {
            $array["item$itemcount"]['text'] = (string) $v;
            $array["item$itemcount"]['command'] = (string) $v['command'];
            $itemcount++;
        }

        if ((string) $k == 'nest')
        {
            $array["nest$nestcount"] = array('name' => (string) $v['name'], 'items' => array());
            $nestcount++;

            $array["nest$nestcount"]['items'] = self::subMenuRecursionArray($xml->nest, $array["nest".($nestcount-1)]['items'], $itemcount, $nestcount);
        }
    }
    return $array;
  }
}

$xml_fragment = '
<menu>
<item command="DefaultCommand">Main (Not nested 1)</item>
<item command="DefaultCommand">Main 2 (Not nested 2)</item>
<nest name="Cont">
    <item command="Contact">NESTED Contact 1</item>
    <item command="Contact">NESTED Contact 2</item>
  </nest>
</menu>';

$xml = simplexml_load_string($xml_fragment);
$array = Parser::subMenuRecursionArray($xml);
echo '<pre>' . print_r($array, 1) . '</pre>';
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜