JQUERY / AJAX PARSE XML TO TABLE


/ Published in: jQuery
Save to your folder(s)

jQuery Ajax function to load xml data into a table and add class to even rows for striping.


Copy this code and paste it in your HTML
  1. $(document).ready(function() {
  2. $.ajax({
  3. type: "GET",
  4. url: "data.xml",
  5. dataType: "xml",
  6. success: parseXml
  7. });
  8. function parseXml(xml) {
  9. $(xml).find("location").each(function() {
  10. //find each instance of popupTitle in xml file
  11. var popupTitle = $(this).find("popupTitle").text();
  12. //if blank hide row
  13. if (popupTitle == "") {
  14. $("table#table tbody tr").hide();
  15. } else {
  16. //else add tr and td tags and add alt class for striping
  17. $("table#table tbody").append("<tr>" + "<td nowrap='nowrap'>" + $(this).find("popupTitle").text() + "</td>" + "<td>" + $(this).find("popupDescription").text() + "</td>" + "</tr>");
  18. $('table#table tbody tr:even').addClass('alt');
  19. }
  20. });
  21. }
  22. });

URL: http://www.vagrantradio.com/2009/10/how-to-parse-xml-using-jquery-and-ajax.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.