0

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?

3
  • Take a look over simplexml_load_string Commented Jan 16, 2013 at 11:44
  • you do $movies->offer which means there has to be a child node "offer" below the root node "pasaz:Envelope". If you look at your XML, you will see that this is not the case. So no surprise this ain't working. Have a look at php.net/manual/en/simplexml.examples-basic.php Commented Jan 16, 2013 at 11:48
  • Also make sure you understand how to work with namespaces. Commented Jan 16, 2013 at 11:59

1 Answer 1

2

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) { .. }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.