Skip to content

Instantly share code, notes, and snippets.

@ehynds
Created August 27, 2010 20:35
Show Gist options
  • Select an option

  • Save ehynds/554152 to your computer and use it in GitHub Desktop.

Select an option

Save ehynds/554152 to your computer and use it in GitHub Desktop.
// the correct way to use jQuery w/ XML
// also see http://gist.github.com/553364 for a normalized DOMParser
var
// XML string
xmlString = '<wu_tang><member name="Method Man" /></wu_tang>',
// DOM parsing object
parser = new DOMParser(),
// XML DOM object
xmlObject = parser.parseFromString(xmlString , "text/xml");
// this is WRONG. It works, but you're not on an XML DOM
$( xmlString ).find("member").attr("name"); // -> Method Man
// the correct way
$( xmlObject ).find("member").attr("name"); // -> Method Man
// in XHR requests the parsing is automatically done for you by
// the browser. jQuery passes it into the success callback
$.ajax({
dataType: 'xml',
url: 'wutang.xml',
success: function( XMLObject ){
// OMG
$( xmlObject ).find("member").attr("name"); // -> Method Man
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment