XML-Tag-Gruppenattribute in PHP
Nehmen wir nun an, wir haben mehrere Tags mit Attributen:
<root>
<tag attr="val1">text1</tag>
<tag attr="val2">text2</tag>
<tag attr="val3">text3</tag>
</root>
Lassen Sie uns die Texte dieser Tags abrufen:
<?php
foreach ($xml->tag as $tag) {
echo $tag; // 'text1', 'text2', 'text3'
}
?>
Und nun - die Attributwerte:
<?php
foreach ($xml->tag as $tag) {
echo $tag['attr']; // 'val1', 'val2', 'val3'
}
?>
Gegeben ist das folgende XML:
<root>
<product cost="100" amount="3">
prod1
</product>
<product cost="200" amount="4">
prod2
</product>
<product cost="300" amount="5">
prod3
</product>
</root>
Geben Sie den Namen, den Preis und die Menge jedes Produkts aus.