1

I have the following xml file and i am using php simplexml to parse the data:

<?xml version="1.0"?>
<root>
<update_time>2014-06-09</update_time>
<item>
<url>http://www.myproducts.com</url>
<name>Wireless Bluetooth Speaker - White</name>

<category>
<parent_id>53</parent_id>
<parent_name>Electronics</parent_name>

<category_id>150</category_id>
<category_name>Mini Speaker</category_name>

<parent_id>81</parent_id>
<parent_name>Bluetooth Function Devices</parent_name>

<category_id>1384</category_id>
<category_name>Bluetooth Function Speaker</category_name>
</category>

<media>
<images>http://www.myshop.com/media/catalog/product/f/i/file_2111_316.jpg</images>
<images>http://www.myshop.com/media/catalog/product/f/i/file_2112_260.jpg</images>
</media>

<short_description>
<![CDATA[<p><span style="font-size: small;"><span style="font-family: Arial;">This mini speaker is in unique cup appearance model design. </span></span></p>]]>
</short_description>

<description>
<![CDATA[<p><strong><span style="font-size: small;"><span style="font-family: Arial;">Features:</span></span></strong>
* Special tablet PC<br />
* Protect your tablet screen from dust and hand print<br />
* Size: 17.3 x 10.5cm</span></span></p>
<p><span style="font-size: small;"><span style="font-family: Arial;">Package included:<br />
1* Screen protective film <br />
</span></span></p>]]>
</description>

</item>
</root>

My php code is as follows:

<?php
        if(file_exists('feed.xml'))
        {
        $xml = simplexml_load_file('feed.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
        }
        foreach($xml->item as $products)
        {

        echo 'URL:' . (string)$products->url .'<br />';
        echo 'Name:' . (string)$products->name .'<br />';           
        echo 'Category_ID:' . (string)$products->category->category_id .'<br />';//for subcategory
        echo 'Category_Name:' . (string)$products->category->category_name .'<br />';
        echo 'Images:' . (string)$products->media->images .'<br />';
        echo 'Short Description:' . strip_tags((string)$products->short_description) .'<br />'.'<br />';
        echo 'Description:' . strip_tags((string)$products->description) .'<br />';
        echo '<br />';

        }   

I am getting the elements correctly except items under node - category and media. category has multiple category_id and category_name items, similarly media has multiple images.

With my present code I am able to get only the first item in both

 category->category_id,  (category_id =150)

 category->category_name  (Category_Name:Mini Speaker )

 media->images   (http://www.myshop.com/media/catalog/product/f/i/file_2111_316.jpg)

What am i missing ??? Requesting help...

user3737431
  • 49
  • 10

1 Answers1

1

Instead of casting to string, cast to array:

$category_id = (array) $products->category->category_id;
$category_name = (array) $products->category->category_name;

So you'll get an array of values to work with.

If you only want to show those values use implode:

echo 'Category_ID:' . implode( ' ', (array) $products->category->category_id ) . '<br />';
Danijel
  • 12,408
  • 5
  • 38
  • 54