| |
PROGRAMMING COURSES
ADMIN VIDEO TUTORIALS
COMMUNITY RESOURCES
|
|
|
Read XML with jQuery |
Programming Category: jQuery
Author: pch Added: Feb 17, 2010 Views: 1521 |
 |
I've been making apps with jQuery the last few tutorials but it is time
to get back to the basics. I can't find a reason to use xml instead of
a database because it's slow and hard to manilpulate (not with jQuery
luckily) but maybe this tutorial will help some of you. 1. The jQuery You could use either the $.get method or $.ajax method to get data from an xml document. Of course $.ajax is much more powerful but for a simple task $.get will do.
$.get( "members.xml", function(xml){ $(xml).find('user').each(function(){ var userID = $(this).attr("id"); var name = $(this).find("name").text(); var country = $(this).find("country").text();
$("#content").append('<div class="name"><a
href="/webmaster.php?id=' + userID + '" target="_blank">' + name +
'</a></div><div class="country">' + country +
'</div>'); });
It is this simple. With normal Javascript you would need much more lines of code. This code above is enough to get you started.
$(xml).find('user').each(function(){ = Find the tag name user and for each tag do the following....
var userID = $(this).attr("id"); = variable userID is the attribute id
var name = $(this).find("name").text(); = variable name is the text in the tag name in the xml document
$("#content").append('<div class="name">... = Add <div class=..... to the div called content
DEMO: http://tutcenter.net/tutorialFiles/readXML/
|
|
|