douxun7992 2019-02-13 01:41
浏览 20
已采纳

递归迭代SimpleXML对象*,其中结构未知*

Every example of xml iteration I've found online (including PHP docs, W3Schools, and a stackoverflow search) presumes that we know the structure ahead of time. I would like to create a loop that iterates as deep as it can go in every branch and simply returns the node names and values that it finds. For example:

<za-lord>
    <orderid>dresden1234</orderid>
    <customer>toot-toot</customer>
    <pizza>
        <sauce>marinara</sauce>
        <crust>thin</crust>
        <toppings>
            <cheese>extra</cheese>
            <veg>
                <onions>yes</onions>
                <peppers>extra</peppers>
                <olives>no</olives>
            </veg>
            <meat>
                <groundbeef>yes</groundbeef>
                <ham>no</ham>
                <sausage>no</sausage>
            </meat>
        </toppings>
    </pizza>
</za-lord>  

What I'm looking for, then, is:

orderid = dresden1234
customer = toot-toot
sauce = marinara
crust = thin
cheese = extra
onions = yes
peppers = extra
olives = no
groundbeef = yes
ham = no
sausage = no 

I've spent a couple hours now writing code examples, testing different variations on foreach, and the short version is that nothing is getting me what I want. Not knowing the structure ahead of time, is it possible to recursively iterate the xml above and return node names and values using SimpleXML, and if so, how?

  • 写回答

1条回答 默认 最新

  • du6333137 2019-02-13 02:02
    关注

    You can use a SimpleXMLIterator object and recurse over it to get all the node values:

    function list_nodes($sxi) {
        for($sxi->rewind(); $sxi->valid(); $sxi->next() ) {
            if ($sxi->hasChildren()) {
                list_nodes($sxi->current());
            }
            else {
                echo $sxi->key() . " = " . $sxi->current() . "
    ";
            }
        }
    }
    $sxi = new SimpleXMLIterator($xmlstr);
    list_nodes($sxi);
    

    Output:

    orderid = dresden1234 
    customer = toot-toot 
    sauce = marinara 
    crust = thin 
    cheese = extra 
    onions = yes 
    peppers = extra 
    olives = no 
    groundbeef = yes 
    ham = no 
    sausage = no
    

    Demo on 3v4l.org

    Update

    If your xml can have namespaces, you have to take a more complicated approach, checking each node for children in each of the namespaces in the document:

    function list_children($node, $names) {
        $children = false;
        foreach ($names as $name) {
            if (count($node->children($name))) {
                $children = true;
                foreach ($node->children($name) as $child) {
                    list_children($child, $names);
                }
            }
        }
        if (!$children) {
            echo $node->getName() . " = $node
    ";
        }
    }
    
    $xml = new SimpleXMLElement($xmlstr);
    list_children($xml, array_merge(array(''), $xml->getNamespaces(true)));
    

    Output (for the demo xml, same as the question but with namespaces added):

    orderid = dresden1234 
    customer = toot-toot 
    sauce = marinara 
    crust = thin 
    cheese = extra 
    onions = yes 
    peppers = extra 
    olives = no 
    ham = no 
    sausage = no
    groundbeef = yes 
    

    Demo on 3v4l.org

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了