Gruppo di tag XML con annidamento in PHP
Supponiamo ora di avere diversi tag che contengono all'interno elementi annidati:
test.xml<root>
<tag>
<elem>text1</elem>
</tag>
<tag>
<elem>text2</elem>
</tag>
<tag>
<elem>text3</elem>
</tag>
</root>
Iteriamo i nostri tag con un ciclo e stampiamo i valori dei tag annidati:
index.php<?php
foreach ($xml->tag as $tag) {
echo $tag->elem; // 'text1', 'text2', 'text3'
}
?>
Задание 1
Dato il seguente XML:
html<root>
<user>
<name>john</name>
<surn>smit</surn>
</user>
<user>
<name>eric</name>
<surn>wils</surn>
</user>
<user>
<name>kyle</name>
<surn>tayl</surn>
</user>
</root>
Stampa a schermo il nome e il cognome di ogni utente.
Задание 2
Dato il seguente XML:
html<root>
<product cost="100" amount="3">
<name>prod1</name>
<category>cat1</category>
</product>
<product cost="200" amount="4">
<name>prod2</name>
<category>cat2</category>
</product>
<product cost="300" amount="5">
<name>prod3</name>
<category>cat3</category>
</product>
</root>
Stampa a schermo il nome, la categoria, il prezzo e la quantità di ogni prodotto.