Results 1 to 3 of 3

Thread: Retrieving XML data from Dynamic XML file

  1. #1
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Retrieving XML data from Dynamic XML file

    Hi everyone,

    I'm simply trying to grab a simple element from an XML.

    The XML has the following format:



    Code:
    <geonames>
    <geoname>
    <toponymName>Grand Tikal Futura Hotel</toponymName>
    <name>Grand Tikal Futura Hotel</name>
    <lat>14.618</lat>
    <lng>-90.5249</lng>
    <geonameId>6475052</geonameId>
    <countryCode>GT</countryCode>
    <countryName>Guatemala</countryName>
    <fcl>S</fcl>
    <fcode>HTL</fcode>
    <distance>1.23124</distance>
    </geoname>
    </geonames>
    My PHP is currently like this (note the link to the XML is calling two PHP variables as part of it's URL)

    PHP Code:
    <?PHP
    header
    ('Content-Type: text/plain');

    $userId '-USERID';

    if ( 
    $_GET['user'] ) {
    if ( 
    is_numeric$_GET['user'] ) )
    $userId $_GET['user'];
    else
    exit(
    'This isn\'t a valid user id.');
    }

    $url 'http://www.google.com/latitude/apps/badge/api?user='.$userId.'&type=json';

    // We get the content
    $content file_get_contents$url );

    // We convert the JSON to an object
    $json json_decode$content );

    $coord $json->features[0]->geometry->coordinates;
    $timeStamp $json->features[0]->properties->timeStamp;

    if ( ! 
    $coord )
    exit(
    'This user doesn\'t exist.');

    $date date'Y-m-d H:i'$timeStamp );
    $lat $coord[1];
    $lon $coord[0];


    $dom = new DomDocument();
    $dom->load("http://www.URL.com?lat=$lat&lng=$lon&username=USER&style=short"); 
    $titles $dom->getElementsByTagName("geonameId");
    foreach(
    $titles as $node) {
       print 
    $node->geonameId" ";
       
    }


    ?>
    I've been trying and searching all morning, lots of different ideas, but with no luck. At the moment, for simplicity, I'm simply trying to output the needed field "GeonameId". I would really appreciate some help, and apologies.... I am, quite obviously, a novice! Thanks!
    Last edited by klous-1; 07-26-2011 at 08:02 PM.

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You have multiple problems with your code.

    1. You're not accessing the node "geonameId" properly. You're trying to access it directly, as if it were the parent. The parent node of "geonameId" is "geoname", and "geoname" has a parent named "geonames" (so it is nested twice).

    2. You need to access your node with item({count}), otherwise it will not access anything except the object itself.

    3. You need to use nodeValue to access any node value. Any node names are accessed through getElementsByTagName() or getElementsById() (so $node->geonameId is not valid).

    Also, you're looping through the nodes as if there are multiple nodes wih the name "geonameId", but your XML file contains only one. I kept the loop in there in case you do in fact need it, and I also added another line of code to access that node once on the zero level.

    This should work for you:
    PHP Code:
    <?PHP
    header
    ('Content-Type: text/plain');

    $userId '-USERID';

    if ( 
    $_GET['user'] ) {
    if ( 
    is_numeric$_GET['user'] ) )
    $userId $_GET['user'];
    else
    exit(
    'This isn\'t a valid user id.');
    }

    $url 'http://www.google.com/latitude/apps/badge/api?user='.$userId.'&type=json';

    // We get the content
    $content file_get_contents$url );

    // We convert the JSON to an object
    $json json_decode$content );

    $coord $json->features[0]->geometry->coordinates;
    $timeStamp $json->features[0]->properties->timeStamp;

    if ( ! 
    $coord )
    exit(
    'This user doesn\'t exist.');

    $date date'Y-m-d H:i'$timeStamp );
    $lat $coord[1];
    $lon $coord[0];

    $dom = new DomDocument();
    $dom->load("test.xml"); 
    $geonames $dom->getElementsByTagName("geonames")->item(0);
    $geoname $geonames->getElementsByTagName("geoname")->item(0);
    $titles $geoname->getElementsByTagName("geonameId");
    $c 0// This is the counter.

    // This will print all of the nodes named "geonameId".
    foreach($titles as $node) {
       print 
    $titles->item($c)->nodeValue" ";
       
    $c++; // Increase the counter for each node.
    }

    // This will print the first node value.  You should use this if you have only one node named "geonameId".
    print $titles->item(0)->nodeValue" ";

    ?>
    ... and it will return all of the values of the nodes named "geonameId" within geoname within geonames.

    Here's some documentation on DomElements.
    http://www.php.net/manual/en/class.domdocument.php

  3. The Following User Says Thank You to JShor For This Useful Post:

    klous-1 (07-30-2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Wow! Great reply....

    It was really good of you to take time out to write that...must have taken a little while....I'm really grateful! Cheers!

    Let me try all this out....and I'll let you know how I go!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •