Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4

Thread: XML DOM

  1. #1
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    Im trying to extract 2 pieces of data from a XML file below:

    Code:
    <ResultSet xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
    ?
        <Result precision="zip">
    <Latitude>37.975715</Latitude>
    <Longitude>-0.680609</Longitude>
    <Address/>
    <City>Torrevieja</City>
    <State>Spain</State>
    <Zip/>
    <Country>ES</Country>
    </Result>
    </ResultSet>
    Ive tried using a script I found on the W3C schools site which isnt working, what do I need to change

    PHP Code:
    <?php
    $xml 
    simplexml_load_file("http://api.local.yahoo.com/MapsService/V1/geocode?appid=capelinks&location=torrevieja&Geocode=Geocode");

    echo 
    $xml->getName() . "<br />";

    foreach(
    $xml->children() as $child)
      {
      echo 
    $child->getName() . ": " $child "<br />";
      }
    ?>
    Regards Phil,


  2.  

  3. #2
    Member
    Join Date
    Apr 2007
    Posts
    96
    Member #
    15165
    Quote Originally Posted by leprechaun13
    Im trying to extract 2 pieces of data from a XML file below:

    Code:
    <ResultSet xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
    ?
        <Result precision="zip">
    <Latitude>37.975715</Latitude>
    <Longitude>-0.680609</Longitude>
    <Address/>
    <City>Torrevieja</City>
    <State>Spain</State>
    <Zip/>
    <Country>ES</Country>
    </Result>
    </ResultSet>
    Maybe you could get away with just using a regular expression.
    PHP Code:
    //updating this code per shadowfiends comments below
    $pattern '/\<Latitude\>(.+)\<\/Latitude\>/';
    preg_match($pattern$xml$match);
    print_r($match); 
    Note. I wrote the pattern off the top of my head. It might be a little off on its escaping special characters. (or not, sometimes I get it right the first time).

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    The first paren shouldn't be escaped, and all the backslashes probably should be, but other than that it looks right.

    What isn't working about the DOM usage?

  5. #4
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    Ive got it working, with a script I found somewhere else, which is

    PHP Code:
    <?
        $loc 
    $_GET['l'];
      
    // DOMElement->getElementsByTagName() -- Gets elements by tagname
      // nodeValue : The value of this node, depending on its type.
      // Load XML File. You can use loadXML if you wish to load XML data from a string

      
    $objDOM = new DOMDocument();
      
    $objDOM->load("http://api.local.yahoo.com/MapsService/V1/geocode?appid=capelinks&location=$loc&Geocode=Geocode"); //make sure path is correct


        
    $result $objDOM->getElementsByTagName("ResultSet");
      
    // for each note tag, parse the document and get values for
      // tasks and details tag.
        
    $result $objDOM->getElementsByTagName("Result");
      
    $i 0;
     
      foreach( 
    $result as $value )
      {

        
    $lats $value->getElementsByTagName("Latitude");
        
    $lat  $lats->item(0)->nodeValue;

        
    $longs $value->getElementsByTagName("Longitude");
        
    $long  $longs->item(0)->nodeValue;

        
    $lat[$i] = $lat;
        
    $long[$i] = $long;
      }
      
    $pos $lat "," $long;
    ?>
    the script is used to get the lat and long for a location and input them into google maps. Ive attached the entire script.
    Regards Phil,



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:11 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com