SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict The Mog's Avatar
    Join Date
    Dec 2002
    Location
    Manchester UK
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy php and XML ,dont know whats wrong

    hi guys,

    I have the following, and i cant seem to understand whats the problem, its not showing the data from the XML file???

    I guess its something small but i am overlooking something somewhere

    PHP Code:
     
    <?php
     
    if ($_GET['lat'] != "" && $_GET['lng'] != ""){
     
    $file utf8_encode("http://ws.geonames.org/findNearbyPlaceName?lat=".$_GET['lat']."&lng=".$_GET['lng']."&style=full");
     
    $content false;
     
    $tag "";
     
    $name "";
     
    $countryName "";
     
    $adminName1 "";
     function 
    startElement($parser$tagname$attrs) {
        global 
    $content$tag$name$countryName$adminName1;
        if (
    $content) {
          
    $tag $tagname;
        } elseif (
    $tagname == "geonames") {
          
    $content true;
        }
      }
      function 
    endElement($parser$tagname) {
          global 
    $content$tag$name$countryName$adminName1;
          if (
    $tagname == "geonames") {
            
    // printf("<a href='%s'>%s</a>",trim($url),trim($name));
             
    print "Name: ".$name."<br>";
             print 
    "Admin Name: ".$adminName1."<br>";
             print 
    "Country: ".$countryName."<br>";
             
    $name "";
             
    $countryName "";
             
    $adminName1 "";
             
    $content false;
          }
      }
      function 
    characterData($parser$data) {
        global 
    $content$tag$name$countryName$adminName1;
          if (
    $content) {
            switch (
    $tag) {
            case 
    "name":
              
    $name .= $data;
              break;
            case 
    "countryName":
              
    $countryName .= $data;
              break;
            case 
    "adminName1":
              
    $adminName1 .= $data;
              break;
            }
          }
        }
      
    $xml_parser xml_parser_create();
      
    xml_set_element_handler($xml_parser"startElement""endElement");
      
    xml_set_character_data_handler($xml_parser"characterData");
      
    $fp fopen($file,"r")or die("Cant open file");// The name and location of your XML file
      
    while ($data fread($fp4096)) //4mb chunks recomended
        
    if(!(xml_parse($xml_parser$datafeof($fp)))){
        die(
    "Error on line " xml_get_current_line_number($xml_parser));
        }
        
    fclose($fp);
        
    xml_parser_free($xml_parser);
     }
     
    ?>
    Any help will be great

    Thanks

    Kenny

  2. #2
    SitePoint Addict The Mog's Avatar
    Join Date
    Dec 2002
    Location
    Manchester UK
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guys,

    I found the problem,

    for some reason you have to use CAPS ??? Can someone please explain why to me?

    PHP Code:
     if ($_GET['lat'] != "" && $_GET['lng'] != ""){
     
    $file utf8_encode("http://ws.geonames.org/findNearbyPlaceName?lat=".$_GET['lat']."&lng=".$_GET['lng']."&style=full");
     
    $content false;
     
    $tag "";
     
    $name "";
     
    $countryName "";
     
    $adminName1 "";
     function 
    startElement($parser$tagname$attrs) {
        global 
    $content$tag$name$countryName$adminName1;
        if (
    $content) {
          
    $tag $tagname;
        } elseif (
    $tagname == "GEONAMES") {
          
    $content true;
        }
      }
      function 
    endElement($parser$tagname) {
          global 
    $content$tag$name$countryName$adminName1;
          if (
    $tagname == "GEONAMES") {
            
    // printf("<a href='%s'>%s</a>",trim($url),trim($name));
             
    print "Name: ".$name."<br>";
             print 
    "Admin Name: ".$adminName1."<br>";
             print 
    "Country: ".$countryName."<br>";
             
    $name "";
             
    $countryName "";
             
    $adminName1 "";
             
    $content false;
          }
      }
      function 
    characterData($parser$data) {
        global 
    $content$tag$name$countryName$adminName1;
          if (
    $content) {
            switch (
    $tag) {
            case 
    "NAME":
              
    $name .= $data;
              break;
            case 
    "COUNTRYNAME":
              
    $countryName .= $data;
              break;
            case 
    "ADMINNAME1":
              
    $adminName1 .= $data;
              break;
            }
          }
        }
      
    $xml_parser xml_parser_create();
      
    xml_set_element_handler($xml_parser"startElement""endElement");
      
    xml_set_character_data_handler($xml_parser"characterData");
      
    $fp fopen($file,"r")or die("Cant open file");// The name and location of your XML file
      
    while ($data fread($fp4096)) //4mb chunks recomended
        
    if(!(xml_parse($xml_parser$datafeof($fp)))){
        die(
    "Error on line " xml_get_current_line_number($xml_parser));
        }
        
    fclose($fp);
        
    xml_parser_free($xml_parser);
     }
     
    ?> 
    Thanks

    Kenny

  3. #3
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I've not had much experience with XML, but with the XML I've seen, the tags are always in CAPS.

    So, if PHP's string comparisons are case-sensitive, it's probably why you have to use CAPS for your tag name comparison.

    Would be my guess
    No, I REALLY dislike having to use Joomla.


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
  •