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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题