PHP - Simple XML - Nested Hierarchy

后端 未结 1 1576
天命终不由人
天命终不由人 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 <noteproperties>

    Check this out, example from SimpleXMLElement::children:

    $xml = new SimpleXMLElement(
    '<person>
         <child role="son">
             <child role="daughter"/>
         </child>
         <child role="daughter">
             <child role="son">
                 <child role="son"/>
             </child>
         </child>
     </person>');
    
    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)
提交回复
热议问题