peter88uk
peter88uk

Reputation: 327

Issues importing XML with Laravel Package orchestral/parser

I have this XML that i'm trying to import:

    <products>
<product product_id="62" title="Product Title" description="<p>desc</p>" price="40" brand="" weight="100" in_stock="Y"/>
<product product_id="63" title="Product Title" description="<p>desc</p>" price="40" brand="" weight="100" in_stock="Y"/>
</products>

I'm using Laravel 5 and this package: https://github.com/orchestral/parser

So i'm running this code:

    $xml = XmlParser::load('https://www.url/feed.xml');


    $product = $xml->parse([
    'product_id' => ['uses' => 'product::product_id'],
    'title' => ['uses' => 'product::title'],
    'description' => ['uses' => 'product::description'],
]);
    dd($product);

But it's just returning a single product in an array which isn't even the first or last one in the XML. So i'm a bit confused.

Anyone got any ideas?

Thanks.

Upvotes: 3

Views: 2691

Answers (2)

Alleen Wang
Alleen Wang

Reputation: 41

You can try "simplexml_load_string()" on Laravel.

When you use this function to parser xml file, you don't need to include other plugin on your Laravel project.

Reference : http://www.w3schools.com/php/func_simplexml_load_string.asp

For example :

$test_strting="<posts>
                   <post>
                       <title>Title1</title>
                   </post>
                   <post>
                       <title>Title2</title>
                   </post>
               </posts>";

$xml = simplexml_load_string($test_strting);

foreach ($xmltest->post as $key => $value) 
{
    $title = (string) $value->title;
    echo("Title: ".$title);
}

Upvotes: 0

peter88uk
peter88uk

Reputation: 327

What I did in the end was:

$xml = XmlParser::load('https://www.url/feed.xml');
$products = $xml->getContent();

foreach($products as $product) {

$item->title = $product['title'];
$item->save();
}

Upvotes: 5

Related Questions