doq91130 2017-12-13 16:11
浏览 54
已采纳

我需要一个递归的php函数来循环遍历xml文件

I'm trying to loop through a xml file and save nodes pared with it's value into an array (key => value). I also want it to keep track of the nodes it passed (something like array(users_user_name => "myName", users_user_email => "myEmail") etc.).

I know how to do this but there is a problem. All the nodes could have children and those children might also have children etc. so I need some sort of recursive function to keep looping through the children until it reaches the last child.

So far I got this:

//loads the xml file and creates simpleXML object
        $xml = simplexml_load_string($content);

        // for each root value
        foreach ($xml->children() as $children) {
            // for each child of the root node
            $node = $children;
            while ($children->children()) {
                foreach ($children as $child) {

                    if($child->children()){
                        break;
                    }
                    $children = $node->getName();
                    //Give key a name
                    $keyOfValue = $xml->getName() . "_" . $children . "_" . $child->getName();
                    // pass value from child to children
                    $children = $child;

                    // no children, fill array: key => value
                    if ($child->children() == false) {
                        $parent[$keyOfValue] = (string)$child;
                    }
                }
            }
            $dataObject[] = $parent;
        }

The "break;" is to prevent it from giving me the wrong values because "child" is an object and not the last child.

  • 写回答

3条回答 默认 最新

  • dongmijgnnq0118 2017-12-14 12:26
    关注

    Using recursion, you can write some 'complicated' processing, but the problem is loosing your place.

    The function I use here passed in a couple of things to keep track of the name and the current output, but also the node it's currently working with. As you can see - the method checks if there are any child nodes and calls the function again to process each one of them.

    $content = <<< XML
    <users>
        <user>
            <name>myName</name>
            <email>myEmail</email>
            <address><line1>address1</line1><line2>address2</line2></address>
        </user>
    </users>
    XML;
    
    function processNode ( $base, SimpleXMLElement $node, &$output )  {
        $base[] = $node->getName();
        $nodeName = implode("_", $base);
        $childNodes = $node->children();
        if ( count($childNodes) == 0 )  {
            $output[ $nodeName ] = (string)$node;
        }
        else    {
            foreach ( $childNodes as $newNode ) {
                processNode($base, $newNode, $output);
            }
        }
    }
    
    $xml = simplexml_load_string($content);
    $output = [];
    processNode([], $xml, $output);
    print_r($output);
    

    This prints out...

    Array
    (
        [users_user_name] => myName
        [users_user_email] => myEmail
        [users_user_address_line1] => address1
        [users_user_address_line2] => address2
    )
    

    With this implementation, there are limitations to the content - so for example - repeating content will only keep the last value (say for example there were multiple users).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题