Jump to content
jasonc

how to correctly access an XML node ?

Recommended Posts

Please can someone advise how I correctly access each of the nodes in an XML file.

 

echo($geoLocXML->isp . '<br>');

this is what I used before and it worked up to last month, then all of a sudden it failed.  I have also tried this code on a different server and it fails there too, so it suggests there is an error in the code, but for the life of me I can not find it.

 

 

 

<?php                //exit;

error_reporting(-1);

//$ip = $_SERVER['REMOTE_ADDR'];
//$geoLocXML = getGeoLocXML($ip); //->result[0];
//$geoLocXML = file_get_contents('http://api.geoiplookup.net/?query='.$ip);
$getGeoLocXML = simplexml_load_file('http://www.mysite.com/getGeoLocXML.xml');

/*
$getGeoLocXML = '<?xml version="1.0" encoding="iso-8859-1"?        >
<ip>
<results><result><ip>123.123.123.123</ip><host>123.123.123.123</host><isp>Provider</isp><city>My City</city><countrycode>GB</countrycode><countryname>United Kingdom</countryname><latitude>123.123</latitude><longitude>-1.123</longitude></result></results>
</ip>
';
*/

$geoLocXML = $getGeoLocXML->result[0];
echo($geoLocXML . '<br>');
echo($geoLocXML->isp . '<br>');
echo($geoLocXML[city] . '<br>');
echo($geoLocXML[countryname] . '<br>');

/*
<results>
<result>
<ip>123.123.123.123</ip>
<host>123.123.123.123</host>
<isp>Provider</isp>
<city>My City</city>
<countrycode>GB</countrycode>
<countryname>United Kingdom</countryname>
<latitude>123.123</latitude>
<longitude>-1.123</longitude>
</result>
</results>
*/
?>

 

 

<?xml version="1.0" encoding="iso-8859-1"?>

<ip>
<results><result><ip>123.123.123.123</ip><host>123.123.123.123</host><isp>Provider</isp><city>My City</city><countrycode>GB</countrycode><countryname>United Kingdom</countryname><latitude>123.123</latitude><longitude>-1.123</longitude></result></results>
</ip>

Share this post


Link to post
Share on other sites

Use -> for nodes and [] for attributes (or indexes). So like the ISP value, and both use the arrow.

$geoLocXML = $getGeoLocXML->result[0];
echo($geoLocXML . '<br>');
echo($geoLocXML->isp . '<br>');
echo($geoLocXML->city . '<br>');
echo($geoLocXML->countryname . '<br>');

Share this post


Link to post
Share on other sites

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.