Reputation: 4538
Note: I posted another question about this recently, but it was marked as duplicate because I didn't specify what I had tried clearly enough. I'll try to explain what I did better this time around.
Here's part of the XML file I'm trying to parse:
<entry>
<id>http://www.google.com/m8/feeds/contacts/7ff84d8009d9a0b</id>
<updated>2010-04-11T12:47:03.281Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Name</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='https://www.google.com/m8/feeds/photos/media/7ff84d8009d9a0b/1B2M2Y8AsgTpgAm7PhCfg'/>
<link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/full/7ff84d8009d9a0b'/>
<link rel='edit' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/full/7ff84d8009d9a0b/127099002381000'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/>
</entry>
Here is my JavaScript code:
$("#importData").append("<ul>");
xmlDoc = $.parseXML(data);
$xml = $(xmlDoc);
$xml.find('entry').each(function () {
var name = $(this).find("title").text();
var email = $(this).find('[nodeName="gd:email"]').attr('address');
$("#importData").append("<li>" + name + " - " + email + "</li>");
});
//$(this).text() + " - " + $xml.find("gd:email[address]").index(i)
$("#importData").append("</ul>");
Here is a jsfiddle that attempts (unsuccessfully) to parse the XML: http://jsfiddle.net/DVXB9/4/
Here is another jsfiddle that successfully parses the XML with gd:email
replaced by gdemail
(no colon): http://jsfiddle.net/DVXB9/5/
I have tried various ways to parse my XML with namespaces, including [nodeName=gd:email]
, [nodeName="gd:email"]
, and gd\\:email
as all suggested by this StackOverflow question. However, none of them have worked successfully.
I don't know how I should approach this next. Other people with XML similar in structure to mine have claimed that the aforementioned methods work properly. However, the best I have gotten is an outputted value of "undefined". What am I doing wrong?
Upvotes: 2
Views: 228
Reputation: 6386
Here is updated Fiddle
Just to get your example working I had to add namespace declaration <entry xmlns:gd='http://www.some-url.org'>
otherwise $.parseXML
throw the exception... after that you can use $('gd\\:email, email',$xml)
to get access to the email node.
Upvotes: 2