Question

i trying to get name 'property' atributtes from this sample xml:

<offers>
<offer><id>1578</id>
    <instock>13</instock>
</offer>
<offer><id>1579</id>
    <property name="EAN">634932593250256</property>
    <property name="Dostępne kolory">Niebiesko-Biały</property>
    <property name="Dostępne rozmiary">L</property>
    <instock>1</instock>
</offer>
</offers>

to something like this:

if property dosen't exist
    iD > 1578
if property exist:
    iD > 1579
    EAN > 634932593250256
    Dostępne kolory > Niebiesko-Biały
    Dostępne rozmiary > L

but i don't know how do this :/ maybe someone can help me

my code:

$xml = simplexml_load_file($xmlfile);
$xml_offer = $xml->xpath("//offer");
$total = count($xml_offer);
for($i=0;$i<=$total;$i++){
    echo $xml_offer[$i]->id.' '; 
        foreach ($xml_offer[$i]->children() as $child) {
            echo $child['name'];     
        }
    echo ' <br>';
}

but its only show me the name of childern like 'Dostępne rozmiary' how can i get value?

Was it helpful?

Solution

You don't really need xpath here. You can process this XML using the SimpleXMLElement php class like this:

<pre>
<?php

$xmlStr = '<offers>
<offer><id>1578</id>
    <instock>13</instock>
</offer>
<offer><id>1579</id>
    <property name="EAN">634932593250256</property>
    <property name="Dostępne kolory">Niebiesko-Biały</property>
    <property name="Dostępne rozmiary">L</property>
    <instock>1</instock>
</offer>
</offers>';

$xml = new SimpleXMLElement($xmlStr);

foreach ($xml->offer as $offer)
{
    print_r($offer);
}

Outputs:

SimpleXMLElement Object
(
    [id] => 1578
    [instock] => 13
)
SimpleXMLElement Object
(
    [id] => 1579
    [property] => Array
        (
            [0] => 634932593250256
            [1] => Niebiesko-Biały
            [2] => L
        )

    [instock] => 1
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top