-3

My XML Response look like this:

<Items>
<Item>
 <ImageSets>
   <ImageSet Category="Primary">
      <SwatchImage>
         <URL>http://domain.com/img1.jpg</URL>
         <Height Units="pixels">30</Height>
       </SwatchImage>
       </ImageSet>

      <ImageSet Category="Variant">
       <SwatchImage>
         <URL>http://domain.com/img2.jpg</URL>
         <Height Units="pixels">30</Height>
       </SwatchImage>
       </ImageSet>

      <ImageSet Category="Variant">
       <SwatchImage>
         <URL>http://domain.com/img3.jpg</URL>
         <Height Units="pixels">30</Height>
       </SwatchImage>
      </ImageSet>

      </ImageSets>
      </Item>
      </Items>

Now, How can i get each value of (URL) if there is more than one category??

I need to save all URL in an array, the one that is in primary and all that are in variant. thanks :)

Oswaldo C.
  • 99
  • 7

2 Answers2

0

You're looking for simpleXML.

The code should look something along the lines of:

simplexml_load_string($xml);

foreach ($items as $item) {

/* For each <ImageSet> node, we echo a separate <SwatchImage><URL>. */
foreach ($Item->Item->ImageSets as $ImageSet) {
    echo $ImageSet->SwatchImage->URL;
}
Integerard
  • 13
  • 3
0

Check out accessing attibute from simplexml.

You can access the elements like

$data->{'parent'}->link[0]['href'] (an example)

(That is, the attributes can be accessed using standard array notation - this definitely works on single elements, not sure if it works with the additional index into the element collection.)

Please have a look here for the more details ..

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26