How to use jQuery for XML parsing with namespaces

后端 未结 20 1655
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 10:17

I\'m new to jQuery and would like to parse an XML document.

I\'m able to parse regular XML with the default namespaces but with XML such as:



        
20条回答
  •  醉梦人生
    2020-11-22 10:28

    Found solution in the comment: Parsing XML with namespaces using jQuery $().find

    Using the second half of node name after the colon worked for me. Used .find("lat") instead of .find("geo\:lat") and it worked for me.


    My setup:

    • Chrome 42
    • jQuery 2.1.3

    Sample XML (snippet from Google Contacts API):

    
      http://www.google.com/m8/feeds/contacts/mstefanow%40gmail.com/base/0
      
    
    

    Parsing code:

    var xmlDoc = $.parseXML( xml );
    var $xml = $( xmlDoc );
    var $emailNode = $xml.find( "email" );
    $("#email").html($emailNode.attr("address"));
    

    Plnkr: http://plnkr.co/edit/l8VzyDq1NHtn5qC9zTjf?p=preview

提交回复
热议问题