Jump to content

problems geting values from xml


livewirerules

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/189054-problems-geting-values-from-xml/
Share on other sites

When you var_dump the results you can get a feel of what the xml class does to the xml. I got this:

 

SimpleXMLElement Object
(
    [LOCATION] => SimpleXMLElement Object
        (
            [NAME] => Terrance
            [COUNTRY] => USA
        )

    [FOUND] => SimpleXMLElement Object
        (
            [TOTALOFOUND] => SimpleXMLElement Object
                (
                    [TOTAL] => 3
                )

            [PLACE] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [ADDRESS] => IL Road
                            [NAME] => shop1
                            [postCODE] => 5
                            [CATIM] => SimpleXMLElement Object
                                (
                                    [sMALL] => ILR.jpg
                                    [MEDIUM] => ILR1.jpg
                                    [DESCRIPTION] => feeds
                                )

                            [bUILD] => this is the first test xml feed
                            [iD] => 1235
                            [DIST] => SimpleXMLElement Object
                                (
                                    [LAT] => 25
                                    [LONG] => 547
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [ADDRESS] => Peter Road
                            [NAME] => textas
                            [postCODE] => 987
                            [CATIM] => SimpleXMLElement Object
                                (
                                    [sMALL] => test.jpg
                                    [MEDIUM] => test1.jpg
                                    [DESCRIPTION] => feeds new
                                )

                            [bUILD] => this is the second test xml feed
                            [iD] => 1235
                            [DIST] => SimpleXMLElement Object
                                (
                                    [LAT] => 25
                                    [LONG] => 547
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [ADDRESS] => Thsi is the 3rd st
                            [NAME] => utah
                            [postCODE] => 9117
                            [CATIM] => SimpleXMLElement Object
                                (
                                    [sMALL] => utah.jpg
                                    [MEDIUM] => utah1.jpg
                                    [DESCRIPTION] => feeds new 3
                                )

                            [bUILD] => this is the third test xml feed
                            [iD] => 000000
                            [DIST] => SimpleXMLElement Object
                                (
                                    [LAT] => 000
                                    [LONG] => 54000
                                )

                        )

                )

        )

)

 

from this

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

echo '<pre>';
print_r($xml);
echo '</pre>';

?>


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.