I have an .xml file which looks like this:
<Products>
<Product>
<Denumire>Carcasa rigida THULE Legend GoPro, black (TLGC101)</Denumire>
<Cod>TLGC101</Cod>
<Atribut denumire="Tip">geanta/husa</Atribut>
<Atribut denumire="Compatibilitate">GoPro</Atribut>
<Atribut denumire="Dimensiuni">150 x 210 x 80 mm (exterior), 35 x 185 x 125 mm (interior)</Atribut>
<Atribut denumire="Greutate">0.24 kg</Atribut>
<Atribut denumire="Culoare">negru</Atribut>
<Atribut denumire="Altele">interior spuma EVA</Atribut>
<Atribut denumire="Cod">TLGC101</Atribut>
</Product>
I just can't find a way to extract the values of "Atribut" tags ("geanta/husa", "GoPro" and so on). My code looks like this:
$products = simplexml_load_file("http://abc.xyz/feed.xml");
foreach($products->Product as $prod)
{
foreach($prod->Atribut as $key=>$atr)
{
//extract the value for "denumire" attribute
$nume_atribut = (string)$atr->attributes()[0];
//get the value of the "Atribut" tag
}
}
Dumping the $prod->Atribut gives me:
object(SimpleXMLElement)#6 (8) {
["@attributes"]=>
array(1) {
["denumire"]=>
string(3) "Tip"
}
[0]=>
string(11) "geanta/husa"
[1]=>
string(5) "GoPro"
[2]=>
string(58) "150 x 210 x 80 mm (exterior), 35 x 185 x 125 mm (interior)"
[3]=>
string(7) "0.24 kg"
[4]=>
string(5) "negru"
[5]=>
string(18) "interior spuma EVA"
[6]=>
string(7) "TLGC101"
}
... while dumping $prod->Atribut[0] gives:
object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(1) { ["denumire"]=> string(3) "Tip" } }
Many thanks!
Just use method __toString of SimpleXMLElement directly:
$atr->__toString()
or use type casting (actually it will use __toString internally):
(string)$atr