Jump to content

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>';

?>


This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.