Avatarus
Avatarus

Reputation: 141

Parse XML, get node in PHP using SimpleXMLElement

I have to convert XML file from my supplyer to CSV version (for Magento Store).

I wrote simple php script to do that, but I cant figure it out why its not working (i have other supplyers, but their XML version is much simpler.

But to the point:

Im have this XML

<pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
    <pasaz:Body>
        <loadOffers xmlns="urn:ExportB2B">
            <offers>
                <offer>
                    <id>9</id>
                    <name>
                        <![CDATA[ Luneta celownicza Light Stream 4,5-14x44 30 mm AO ]]>
                    </name>
                    <price>2299</price>
                    <url>
                        <![CDATA[
http://kolba.pl/luneta-celownicza-light-stream-4-5-14x44-30-mm-ao-p9.html
]]>
                    </url>
                    <categoryId>
                        <![CDATA[ Lunety wiatrówkowe ]]>
                    </categoryId>
                    <description>
                        <![CDATA[
description
]]>
                    </description>
                    <image>
                        <![CDATA[
http://kolba.pl/media/images/products/xl_org/p00009.jpg
]]>
                    </image>
                    <availability>1</availability>
                </offer>
                <offer>
                    <id>61</id>
                    <name>
                        <![CDATA[ Szyna- podwyższenie 2 częściowe 11/11 BKL-166 MB ]]>
                    </name>
                    <price>145</price>
                    <url>
                        <![CDATA[
http://kolba.pl/szyna-podwyzszenie-2-czesciowe-11-11-bkl-166-mb-p61.html
]]>
                    </url>
                    <categoryId>
                        <![CDATA[ Montaże ]]>
                    </categoryId>
                    <description>
                        <![CDATA[
description
]]>
                    </description>
                    <image>
                        <![CDATA[
http://kolba.pl/media/images/products/xl_org/p00061.jpg
]]>
                    </image>
                    <availability>1</availability>
                </offer>
            </offers>
        </loadOffers>
    </pasaz:Body>
</pasaz:Envelope>

And im using this script:

$plik=file_get_contents('kolba.xml');
$plik=utf8_encode($plik);
$movies = new SimpleXMLElement($plik);
$stala_czesc='"admin";"base";"Default";"simple";'; 
$stala_czesc2=';"Żaden";0.0000;';
$stala_czesc3='"Not Visible Individually";1;"Włączone"'."\n";   
foreach ($movies->offer as $product) 
{
    //echo '<br>Przetwarzam';
    $wynik="";
    $wynik=$stala_czesc;
    $nazwa=$product->name;
    $nazwa=str_replace('"',"",$nazwa);
    $nazwa = preg_replace("/[^[:alnum:][:punct:] -]/","",$nazwa);
    $wynik.='"'.$product->id.'_kolba";"'.$nazwa.'";
    '.number_format(intval($product->price),4,'.','').';
    '.number_format(intval($product->price),4,'.','').';
    '.number_format(intval($product->price),4,'.','').$stala_czesc2.$product->availability.';'.$stala_czesc3;
    echo '<br>';
    echo $wynik;
}

But FOR iterator never starts, why?

Upvotes: 0

Views: 286

Answers (1)

hank
hank

Reputation: 3768

The problem you are experiencing is because of the namespaced Envelope/Body, here's a dirty hack to get to your data atleast;

$movies = new SimpleXMLElement($plik);
$movies = $movies->children('http://schemas.xmlsoap.org/soap/envelope/');
$offers = $movies->children()->loadOffers->offers;

and to loop the offers

foreach ($offers->offer as $product) { .. }

Upvotes: 2

Related Questions