Dave's Brain

Browse - programming tips - jquery parse xml

Date: 2010jul22
Language: javaScript

Q.  How do you parse XML with jQuery?

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

	text = '<msg><title>The Title</title><info priority="urgent"/></msg>';
	doc = $.createXMLDocument(text); 

	// Does a <tag> exist?
	title = $(doc).find('title');
	if (title.length == 0) {
		// No <tag>
	}

	// Get the text in a <tag>
	title_txt = $(doc).find('title').text();

	// Get an attribute
	priority = $(doc).find('info').attr('priority');

.createXMLDocument is defined in:
http://plugins.jquery.com/project/createXMLDocument
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: