Browse - Programming Tips - How do you parse XML with jQuery?

Date: 2010jul22
Language: javaScript

Q.  How do you parse XML with jQuery?

A.  Use find() text() and attr() and .length like this:

var text = '<msg><title>The Title</title><info priority="urgent"/></msg>'; var doc = $.createXMLDocument(text); // Does a <tag> exist? var title = $(doc).find('title'); if (title.length == 0) { // No <tag> } // Get the text in a <tag> var title_txt = $(doc).find('title').text(); // Get an attribute var priority = $(doc).find('info').attr('priority');
.createXMLDocument is defined in: http://plugins.jquery.com/project/createXMLDocument