You are here:  » Multiple entries within the XML file..


Multiple entries within the XML file..

Submitted by buyowner on Wed, 2008-11-12 16:10 in

I have an XML file, (see below) that has an undefined number of "photo" fields.
Each entry has a different number of photos.
I know that magicparser will parse them as PHOTOS/PHOTO@1 etc.
How can I get a count of those entries and only process the ones I know exist?

Sample XML

<?xml version="1.0" encoding="UTF-8" ?>
<my_feed company_id="ID">
    <listings>
        <property>
            <propid>1234</propid>
            <photos>
                <photo>1234a.jpg</photo>
                <photo>1234b.jpg</photo>
                <photo>1234c.jpg</photo>
                <photo>1234d.jpg</photo>
                <photo>1234e.jpg</photo>
                <photo>1234f.jpg</photo>
            </photos>
        </property>
        <property>
            <propid>5678</propid>
            <photos>
                <photo>5678a.jpg</photo>
                <photo>5678b.jpg</photo>
            </photos>
        </property>
    </listings>
</my_feed>

Thanks
Dan

Submitted by support on Wed, 2008-11-12 16:16

Hello Dan,

The easiest way to handle this style of nested XML within your record handler function is to use a foreach() loop and inspect the key name, and then handle the value if it begins PHOTO/PHOTO, for example (non complete):

<?php
  
function myRecordHandler($record)
  {
    foreach(
$record as $key => $value)
    {
      if (
strpos($key,"PHOTOS/PHOTO") === 0)
      {
        print 
$value;
      }
    }
  }
?>

(of course you would do something different with $value, but that's the general idea!)

Hope this helps,
Cheers,
David.

Submitted by buyowner on Wed, 2008-11-12 18:12

Thanks! That worked great.
Your product has saved me hours of work!