How to read SimpleXML Object in PHP


 
How can I properly parse a SimpleXML Object in PHP?

It is really simple to access attributes using array form. However, you must convert them to strings or ints if you plan on passing the values to functions.

You have to cast simpleXML Object to a string.

$value = (string) $xml->code[0]->lat;

If you know that the value of the XML element is a float number (latitude, longitude, distance), you can use (float)

$value = (float) $xml->code[0]->lat;

Also, (int) for integer number:

$value = (int) $xml->code[0]->distance;

You can more read about simplexmlelement.

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s