Image

Imagebrentmj wrote in Imagephp

Stuck with XML


I have an XML file with a format similar to what follows:

<list>
   <item>
      <title>Item Title</title>
      <desc>Item Description</desc>
      <size>Item Size</size>
      <dailysales>
         <avg>##</avg>
         <max>##</max>
         <min>##</min>
      </dailysales>
      <weeklysales>
         <avg>##</avg>
         <max>##</max>
         <min>##</min>
      </weeklysales>
      <monthlysales>
         <avg>##</avg>
         <max>##</max>
         <min>##</min>
      </monthlysales>
   </item>
   <item>
     .
     .
   </item>
</list>


Having never had to parse XML before, I did some searching and found very quickly and easily how to pull information out of the first node, such as a list of titles, descriptions, or sizes, but the data I want to report is:

Item1: Size, avgMonthlysales
Item2: Size, avgMonthlysales
etc.

preferably with each value in a separate array so they could be easily called as $item[x], $size[x]...

I'm using PHP5, so I've been working with the simplexml functions, although I have worked with the older xml functions. I haven't figured out how to get the data from the monthlysales under each corresponding item.

Any pointers as to where I can look for more information on this? Thanks.


Edit: I'm a little closer, but still need some help. I discovered all of the operators that work with xpath.

Here's my (borrowed) code:

$result = $xml->xpath('item/title | item/size | item/monthlysales/avg');
while (list( ,$node) = each($result)){
echo $node.'
';
}

This gives me a very nice
Item1
Size
avgMonthylsales.

I'd still like to make these into separate arrays because I want to compare them individually with existing database entries, and here is where I'm not smart enough and request help.