Comment optimiser DOMDocument xml
bonjour,
a chaque fois j'utilise un foreach mais comment eviter ça pour lire les élément enfants title et texte ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
$pathImage = 'products/images/';
$xmldatas = new DOMDocument();
$xmldatas->load(PATH_PRODUCTS . '/products.xml');
$xmldatas = $xmldatas->getElementsByTagName("product");
foreach ($xmldatas as $product) {
echo $product->getAttribute('image');
$trans = $product->getElementsByTagName('translation');
foreach ($trans as $tr) {
echo getAttribute('lang');
$title = $tr->getElementsByTagName('title');
foreach ($title as $t) {
echo $t->nodeValue ;
}
$text = $tr->getElementsByTagName('text');
foreach ($text as $txt) {
echo $txt->nodeValue;
}
}
} |
voici fichier xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product id="1" image="thumb-square-1.jpg">
<translation lang="fr">
<title>La box</title>
<text>
<![CDATA[
]]>
</text>
<lien>lien 1</lien>
</translation>
</product>
<product id="2" image="thumb-square-2.jpg">
<translation lang="fr">
<title>Tablette</title>
<text>
<![CDATA[
]]>
</text>
<lien>lien 1</lien>
</translation>
</product>
</products> |