-
-
Save kurtschlatzer/d2acf301ca7e128a9844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Tested with jquery-1.7.2.min.js */ | |
$(window).load(function () { | |
"use strict"; | |
/* Cycles XML file on successful load via ajax and parses contents into HTML */ | |
function parseXML(xml) { | |
/* Variable declaration */ | |
var outHTML = "", employee, sId, sFirstname, sSurname, sAddress; | |
/* For each 'Employee' section in the XML file, extract information */ | |
$(xml).find('Employee').each(function () { | |
/* Extract and set employee details */ | |
employee = $(this); | |
sID = $.trim(employee.find('ID').text()); | |
sFirstname = $.trim(employee.find('Firstname').text()); | |
sSurname = $.trim(employee.find('Surname').text()); | |
sAddress = $.trim(employee.find('Address').text()); | |
/* Build HTML segment to put into the main content */ | |
outHTML += "<div class='employee'>"; | |
outHTML += " <p>ID: " + sID + "</p>"; | |
outHTML += " <p>Name: " + sSurname + ", " + sFirstname + "</p>"; | |
outHTML += " <p>Address: " + sAddress + "</p>"; | |
outHTML += "</div>"; | |
}); | |
/* Append the employee to div.content with previously built outHTML var */ | |
$(".content").append(outHTML); | |
} | |
/* Displays error to div with class of status if one is thrown from the Ajax parser */ | |
function err(XMLHttpRequest, textStatus, errorThrown) { | |
$(".status").append("<strong>TextStatus:</strong> " + textStatus + "<br /><strong>ErrorThrown:</strong> " + errorThrown); | |
} | |
/* Create ajax object for XML parsing */ | |
/* cache: set to false to ensure xml file is loaded new everytime, set to true otherwise */ | |
/* url: must be absolute path */ | |
$.ajax({ | |
type: "GET" | |
, contentType: "application/json; charset=utf-8" | |
, url: "http://www.website.com/file/location/thefile.xml" | |
, dataType: "xml" | |
, cache: false | |
, success: parseXML | |
, error: err | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment