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条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”