SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: PHP xml parse fail :(

  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP xml parse fail :(

    I really can't see why this isn't working. Someone please point out what I'm doing wrong.

    I really don't know why I struggle with this. I get some stuff to work and then try something similar and have to bang my head against the desk.

    Please help.

    PHP Code:
    $output = $output.'<p><a target="_blank" href="'.$xml_link.'">XML link</a></p>';
    $xml = simplexml_load_file($xml_link);
    foreach ($xml as $geoname) {
    $lat = $geoname->lat;
    $lng = $geoname->lng;
    $population = $geoname->population;
    $latlon = $lat.','.$lng;

    }
    $output = $output.'<p>Population '.$population.'</p><p> Latitude /Longitude '.$latlon.'</p>';
    XML I am parsing is:
    <geoname>
    <toponymName>San Javier</toponymName>
    <name>San Javier</name>
    <lat>37.76341</lat>
    <lng>-0.77005</lng>
    <geonameId>6359539</geonameId>
    <countryCode>ES</countryCode>
    <countryName>Spain</countryName>
    <fcl>A</fcl>
    <fcode>ADM3</fcode>
    <fclName>country, state, region,...</fclName>
    <fcodeName>third-order administrative division</fcodeName>
    <population>31432</population>
    <alternateNames/>
    <elevation/>
    <srtm3>-32768</srtm3>
    <continentCode>EU</continentCode>
    <adminCode1>31</adminCode1>
    <adminName1>Murcia</adminName1>
    <adminCode2>MU</adminCode2>
    <adminName2>Murcia</adminName2>
    <adminCode3>30035</adminCode3>
    <adminName3>San Javier</adminName3>
    <alternateName lang="link">http://en.wikipedia.org/wiki/San_Javier%2C_Murcia</alternateName><timezone dstOffset="2.0" gmtOffset="1.0">Europe/Madrid</timezone>
    </geoname>

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,539
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    Try $xml->lat instead. The initial wrapper (geoname) is consumed during the xml parsing.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Location
    Austin
    Posts
    398
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Are you getting an error?

    Here's a good way to validate the file or show errors: http://www.php.net/manual/en/functio...nal-errors.php

    Also you can do a print_r to see the structure if you can't access a node.

  4. #4
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    StarLion Thanks...now I know "The initial wrapper (geoname) is consumed during the xml parsing. ", that helps a great deal.

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
  •