Develop PHP Website for Learning Web Design Free Online
  Home   |   Forums   |   DevCMS
  Join   |   Log In
 
 


Tutor at Develop PHP
Read XML with jQuery
Programming Category: jQuery

Author: pch      Added: Feb 17, 2010      Views: 1521    
Tweet This Page  Post page to Facebook  Post page to Facebook
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/

Comment on Read XML with jQuery

 
 
Arbitrary Links and Archives
Home
Active Forums
Members
SIte News
Link To Us
Gear
2009 Forum Archive
Programming Courses
Learn HTML
Learn CSS
Learn PHP
Learn MySQL
Learn Javascript
Learn jQuery
Learn ActionScript 3.0
Learn Java
Learn XML
DevelopPHP Requires Flash Player
Get Adobe Flash player
___________________________________________

Terms of Use  •  Privacy  •  Admin Notes

©2010 developphp.com   |   Powered By FlashBuildingHolder