Skip to content

Instantly share code, notes, and snippets.

@knkumar
Created June 29, 2012 05:55
Show Gist options
  • Save knkumar/3016102 to your computer and use it in GitHub Desktop.
Save knkumar/3016102 to your computer and use it in GitHub Desktop.
javascript iterate nodeList XML
var doc = xhreq.responseText;
var parser = new DOMParser();
var link = parser.parseFromString(doc, 'text/xml');
var items = link.documentElement.childNodes;
for (var item in items) {
if(item.nodeName == 'link'){
for(var attr in item.attributes.name) {
if(item.attr.name == 'rel'){
//do something awesome
return;
}
}
}
}
@jeevanel
Copy link

//this code should help you iterate and do something with link tag having rel attribute
var items = xmlDoc.documentElement.childNodes;
for (var index=0;index <items.length; index++) {
if(items[index].nodeName == 'link'){
for(var attrIndex=0; attrIndex < items[index].attributes.length; attrIndex++) {
if(items[index].attributes[attrIndex].nodeName == 'rel'){
//do something awesome
alert("found one link with rel attribute");
}
}
}
}

@knkumar
Copy link
Author

knkumar commented Jun 29, 2012

But why is there no iterator object on a nodelist maybe we should write one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment