views:

52

answers:

2

Im trying to get some values from the below xml feed

    <?xml version="1.0" ?>
<SEARCH>
  <LOCATION>
   <NAME>Terrance</NAME>
   <COUNTRY>USA</COUNTRY>
  </LOCATION>
<FOUND>
    <TOTALOFOUND>
    <TOTAL>3</TOTAL>
    </TOTALOFOUND>
  <PLACE>
      <ADDRESS>IL Road</ADDRESS>
      <NAME>shop1</NAME>
      <POSTCODE>5</POSTCODE>
    <CATIM>
      <SMALL>ILR.jpg</SMALL>
      <MEDIUM>ILR1.jpg</MEDIUM>
      <DESCRIPTION>feeds</DESCRIPTION>
    </CATIM>
    <BUILD>this is the first test xml feed</BUILD>
    <ID>1235</ID>
    <DIST>
     <LAT>25</LAT>
     <LONG>547</LONG>
   </DIST>
  </PLACE>
  <PLACE>
      <ADDRESS>Peter Road</ADDRESS>
      <NAME>textas</NAME>
      <POSTCODE>987</POSTCODE>
    <CATIM>
      <SMALL>test.jpg</SMALL>
      <MEDIUM>test1.jpg</MEDIUM>
      <DESCRIPTION>feeds new</DESCRIPTION>
    </CATIM>
    <BUILD>this is the second test xml feed</BUILD>
    <ID>1235</ID>
    <DIST>
     <LAT>25</LAT>
     <LONG>547</LONG>
   </DIST>
  </PLACE>
   <PLACE>
      <ADDRESS>Thsi is the 3rd st</ADDRESS>
      <NAME>utah</NAME>
      <POSTCODE>9117</POSTCODE>
    <CATIM>
      <SMALL>utah.jpg</SMALL>
      <MEDIUM>utah1.jpg</MEDIUM>
      <DESCRIPTION>feeds new 3</DESCRIPTION>
    </CATIM>
    <BUILD>this is the third test xml feed</BUILD>
    <ID>000000</ID>
    <DIST>
     <LAT>000</LAT>
     <LONG>54000</LONG>
   </DIST>
  </PLACE>
 </FOUND>
</SEARCH>

I have used the following code to grab the values

<?php
strings = file_get_contents("feed.xml");
$xml=simplexml_load_string($strings);

foreach ($xml as $place)
{
echo "Total : ".$place->TOTALOFOUND->TOTAL."<br />";
echo "address: ".$place->PLACE->ADDRESS."<br />";
echo "Name : ".$place->PLACE->NAME."<br />";
echo "post code: ".$place->PLACE->POSTCODE."<br />";
echo "Small image: ".$place->PLACE->CATIM->SMALL."<br />";
echo "Medium Image: ".$place->PLACE->CATIM->MEDIUM."<br />";
echo "Descripton: ".$place->PLACE->CATIM->DESCRIPTION."<br />";
echo "Office ID: ".$place->PLACE->ID."<br />";
echo "Cord Lat: ".$place->PLACE->DIST->LAT."<br />";
echo "Cord Long: ".$place->PLACE->DIST->LONG."<br />"."<br />";
}
?>

the problem is that although i have 3 records in the xml it shows only the first results. and at the beginning it gives the below error. Can some one please help me

Total :
address:
Name :
post code:

Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 16
Small image:

Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 17
Medium Image:
Descripton:
Office ID:

Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 20
Cord Lat:

Notice: Trying to get property of non-object in C:\wamp\www\site\test.php on line 21
Cord Long:

Total : 3
address: IL Road
Name : shop1
post code: 5
Small image: ILR.jpg
Medium Image: ILR1.jpg
Descripton:
Office ID: 1235
Cord Lat: 25
Cord Long: 547

any help will be much appreciated

thanks

+3  A: 

You have to look at the structure of the XML more closely and access the nodes in the appropriate way base on their hierarchy.

echo "Total : ".$xml->FOUND->TOTALOFOUND->TOTAL."<br />";
foreach ( $xml->FOUND->PLACE as $place )
{
  echo "address: ".$place->ADDRESS."<br />";
  echo "Name : ".$place->NAME."<br />";
  echo "post code: ".$place->POSTCODE."<br />";
  echo "Small image: ".$place->CATIM->SMALL."<br />";
  echo "Medium Image: ".$place->CATIM->MEDIUM."<br />";
  echo "Descripton: ".$place->CATIM->DESCRIPTION."<br />";
  echo "Office ID: ".$place->ID."<br />";
  echo "Cord Lat: ".$place->DIST->LAT."<br />";
  echo "Cord Long: ".$place->DIST->LONG."<br />"."<br />";
}
Peter Bailey
Thank you.. It works perfectly :D
LiveEn
@LiveEn: Accept the answer if it's the correct one
AntonioCS
You're welcome. And since you are new to StackOverflow, it is customary to vote-up and/or "accept" the reply that answers your question. I invite you to peruse the FAQ http://stackoverflow.com/faq
Peter Bailey
A: 

That's yet another example of why one should always name their PHP vars after the node they represent. This way, you won't get lost in your tree's hierarchy.

The root node is <SEARCH/>, therefore its PHP var should be $SEARCH. Also, please note the use of simplexml_load_file()

$SEARCH = simplexml_load_file('feed.xml');

echo "Total : ".$SEARCH->FOUND->TOTALOFOUND->TOTAL."<br />";

foreach ($SEARCH->FOUND->PLACE as $PLACE)
{
    echo "address: ".$PLACE->ADDRESS."<br />";
    // etc...
    echo "Cord Lat: ".$PLACE->DIST->LAT."<br />";
}
Josh Davis