0

私はPHPの単純なXML関数を使用してXMLファイルを操作してきました。

以下のコードは、単純なXML階層に対して正常に機能します。

$xml = simplexml_load_file("test.xml");

echo $xml->getName() . "<br />";

foreach($xml->children() as $child)
{
    echo $child->getName() . ": " . $child . "<br />";
}

これは、XMLドキュメントの構造が次のとおりであることを前提としています。

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

ただし、XMLドキュメント内にもっと複雑な構造がある場合、コンテンツは単に出力されません。より複雑なXMLの例を以下に示します。

<note>
    <noteproperties>
        <notetype>
            TEST
        </notetype>
    </noteproperties>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

深さが不定のXMLファイルを処理する必要があります-誰かが方法を提案できますか?

4

1 に答える 1

1

それは、もう 1 レベル下に移動する必要があるためです。<noteproperties>

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'];
        }
    }
}
于 2012-04-27T13:47:17.300 に答える