1

this is a bit cheeky, I'm effectively asking you to write code for me but I'm having hard time to extract certain data from a xml result

here is the xml

 <?xml version="1.0" encoding="UTF-8" ?> 
 <GeocodeResponse>
     <status>OK</status> 
     <result>
         <type>route</type> 
         <address_component>
             <long_name>xxxxxxxxxxx</long_name> 
             <short_name>xxxxxxxxxxx</short_name> 
             <type>administrative_area_level_3</type> 
             <type>political</type> 
         </address_component>
     </result>
     <result>
         <type>postal_code</type> 
         <address_component>
            <long_name>xxxxxxxxxxx</long_name> 
            <short_name>xxxxxxxxxxx</short_name> 
            <type>administrative_area_level_2</type> 
            <type>political</type> 
         </address_component>
     </result>
 </GeocodeResponse>

How can I get long_name of type administrative_area_level_3 on address_component of result type route with jquery?

2
  • That last line is supposed to be a close tag for <GeocodeResponse>, right? Commented Dec 12, 2010 at 14:53
  • yes, edited my question to reflect it Commented Dec 12, 2010 at 14:54

2 Answers 2

2

this should work for you

$(document).ready(function () {
    $.ajax({
        type: "GET",
        dataType: "xml",
        url: "XMLFile.xml",
        success: function (xml) {
            $(xml).find('type').each(function () { // find all "type" tag in XML
                if ($(this).text() == 'administrative_area_level_3') { // in here $(this) is our "type" tag
                    var result = $(this).parent().find('long_name').text(); // get result
                    $('#result').html('The Result is: ' + result) // write result
                }
            })
        }
    })
})

live example is here

Sign up to request clarification or add additional context in comments.

7 Comments

thanks, it works fine on ie but not on firefox or pera with dummy xml response. there is no error just nothing happens
it is very strange. I have wrote and tested this code on Firefox with Firebug.
I am sorry but this example is working on all browsers. Firefox Chrome Opera IE9 IE8. I could not test on Safari, however I think it will work on it because Safari is a web-kit browser like Chrome and it is working on Chrome.
noticed that it works with local file. weird it doesn;t if i pull it from the internet.
i think i've found the reason dev.opera.com/articles/view/… it seems IE doesn't obey it
|
0

First, jQuery itself doesn't really parse XML — it lets the browser do it. It can create a DOM fragment and then it allows you to examine that. Thus if your xml is in a string you could use:

var xml = $(xmlString);

Then you could do this:

var longName = null;

xml.children('result').each(function() {
  if ($(this).children('type').text() === 'route') {
    var addr = $(this).find('address_component');
    if (addr.find('type').filter(function() { return this.innerHTML === 'administrative_area_level_3'; }).length())
      longName = addr.children('long_name').text();
  }
});

or something like that anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.