SimpleXML: get child nodes

前端 未结 3 1251
 






    
    
    &         


        
相关标签:
3条回答
  • 2020-12-21 02:47

    Use xpath

    $ccc = $xml->xpath('aaa/bbb/ccc');
    foreach($ccc as $c){
        echo (string)$c."<br/>";
    }
    

    Result:

    Only this childnod what I need 
    And this one 
    

    Also, in your initial attempt, $bbb[3] does not make sense because you have three items only, starting with index 0.

    0 讨论(0)
  • 2020-12-21 02:50

    I don't know how to use the return of simplexml, but from what I have seen from your code, this should work:

    <?php
    header('Content-type:text/html; charset=utf-8');
    $xml = simplexml_load_file('2.xml');
    echo $xml->aaa[2]->bbb[2]->ccc[0];
    echo $xml->aaa[2]->bbb[2]->ccc[1];
    ?> 
    
    0 讨论(0)
  • 2020-12-21 02:54

    A more dynamic solution

    <?php
    header('Content-type:text/html; charset=utf-8');
    $xml = simplexml_load_file('2.xml');
        foreach ($xml->aaa as $aaa){
            if ($aaa->bbb) {
                foreach ($aaa->bbb as $bbb) {
                    if ($bbb->ccc)
                        foreach ($bbb->ccc as $ccc) {
                            echo $ccc;
                        }
                    }
                }
            }
        }
    ?> 
    
    0 讨论(0)
提交回复
热议问题