Web Analytics Made Easy -
StatCounter [Solved] How To Get First Set of Results When Parsing XML - CodingForum

Announcement

Collapse
No announcement yet.

[Solved] How To Get First Set of Results When Parsing XML

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • jQuery [Solved] How To Get First Set of Results When Parsing XML

    I'm trying to parse out location data from XML that is passed back from Google. It currently parses the data correctly, the problem is that it grabs all of the matching tags, instead of just the first results set.

    Here's an example of the output.

    Here's the code I'm using to achieve this:
    Code:
    function(){ 
    var url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=NewBrunswick,NJ&sensor=false';
    $.ajax({url: 'proxy2.php?url='+url, type: "GET", dataType: "text", success: function ($data) 
    {$data = $($data); 
    lat = ($data.find('geometry').find('location').find('lat').text());
    long = ($data.find('geometry').find('location').find('lng').text());
    }});});
    How would I change it so that it only grabs the data from the first set of results?
    Last edited by brando56894; Sep 1, 2011, 04:27 PM.

  • #2
    I'm not familir with xml manipulation but it sounds like eq() should work.


    Code:
    function(){ 
    var url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=NewBrunswick,NJ&sensor=false';
    $.ajax({url: 'proxy2.php?url='+url, type: "GET", dataType: "text", success: function ($data) 
    {$data = $($data); 
    lat = ($data.find('geometry').eq(0).find('location').find('lat').text());
    long = ($data.find('geometry').eq(0).find('location').find('lng').text());
    }});});
    - Firebug is a web developers best friend! - Learn it, Love it, use it!
    - Validate your code! - JQ/JS troubleshooting
    - Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

    Comment


    • #3
      a slightly different approach...

      ... if that fails, it seems to me that you are combining approaches - if you are set on reading the results from xml, maybe you should use something like Xpath... if you want to read it with Jquery, it would seem that getting the results as JSON objects would be better suited. Some documentation

      Comment


      • #4
        .eq was definitely what I needed, thanks!

        I originally tried to do this with JSON since that that's what Google recommends, but my professor and I couldn't (This was part of my final project for class) get the JSON data to be parseable so we switched to XML.

        Comment


        • #5
          Now for a new (related question). How can I access the the variables lat and lng outside of the function? I read somewhere that initializing the variables outside of the function makes them global, yet when I try console.log(lat); in another function it comes back as undefined.

          Code:
              <script type="text/javascript">
          <!-- Passes the geolocation API url to the local proxy server -->
          var lat;
          var lng;
            $(document).ready
            (function()	
          	  { var url = '<?php echo $url;?>';
          	      $.ajax({url: 'proxy2.php?url='+url, type: "GET", dataType: "text", success: function ($data) 
          		{$data = $($data); 
          		lat = ($data.find('geometry').eq(0).find('location').find('lat').text());
          		lng = ($data.find('geometry').eq(0).find('location').find('lng').text());
          
          		}});}); 
          console.log(lat); //outputs: undefined
          				
          //Initalizes Map and Places Pins of Job Locations
          function initialize() 
          {
                 console.log(lat); //outputs: undefined
          	var myLatlng = new google.maps.LatLng(lat,lng);
          	var myOptions = {zoom: 8, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP}
                 [more code here]
          }
              </script>
          Last edited by brando56894; Sep 1, 2011, 05:02 PM.

          Comment

          Working...
          X
          😀
          🥰
          🤢
          😎
          😡
          👍
          👎