SimpleXMLElement
それほど単純ではなく、実際、それはあなたがそれに対処する方法を推測するときだけ簡単になります...または誰かがあなたを提案します。
次regions.xml
のようになります。
<regions>
<region id="01" size="big">
New Zealand
</region>
<region id="02" size="small">
Tokelau
</region>
</regions>
次のPHPコードは、そのXMLドキュメントをナビゲートできます。
$xml = new SimpleXMLElement('regions.xml');
foreach ($xml->region as $region) { // iterate through all the regions
echo 'Region ID: '. (string)$region['id']; // get the attribute id
echo 'Region size: '. (string)$region['size']; // get the attribute size
echo 'Region name: '. (string)$region; // get the contents of the
// element by casting it to a string
}
それでは、何かを少し難しくしましょう...<region>
サブ要素があるとし<subregion>
ます。
<regions>
<region id="01" size="big">
<name>USA</name>
<subregion id="01_1">Alaska</subregion>
</region>
...
</regions>
各リージョンのすべてのサブリージョンを取得する場合は、次のようにする必要があります。
$xml = new SimpleXMLElement('regions.xml');
foreach ($xml->region as $region) { // iterate through all the regions
foreach ($region->subregion as $subregion) // iterate trough the subregion of $region
// do something
}
}
ドキュメントの構造はこれよりも少し複雑に見えますが、これらの基本を使用すると簡単に解決できます。