Dave's Brain

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

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.