PHP - Simple XML - Nested Hierarchy

后端 未结 1 1559
天命终不由人
天命终不由人 2021-01-28 08:12

I have been using PHP\'s simple XML function to work with an XML file.

The below code works fine for a simple XML hierarchy:

$xml = simplexml_load_file(         


        
1条回答
  •  孤独总比滥情好
    2021-01-28 08:33

    That's because you need to go another level down in

    Check this out, example from SimpleXMLElement::children:

    $xml = new SimpleXMLElement(
    '
         
             
         
         
             
                 
             
         
     ');
    
    foreach ($xml->children() as $second_gen) {
        echo ' The person begot a ' . $second_gen['role'];
    
        foreach ($second_gen->children() as $third_gen) {
            echo ' who begot a ' . $third_gen['role'] . ';';
    
            foreach ($third_gen->children() as $fourth_gen) {
                echo ' and that ' . $third_gen['role'] .
                    ' begot a ' . $fourth_gen['role'];
            }
        }
    }
    

    0 讨论(0)
提交回复
热议问题