Read XML files using jQuery

April 05, 2014 | 777 views | Comments

Quickly read and parse XML files with jQuery, no server side scripting required.

For example.

The sample XML file that is used on this tutorial can be viewed on the tutorial Create an XML file with PHP, the generated XML file is named members.xml.

Parse an existing XML file using ajax() function with dataType equals to XML in jQuery.

$.ajax({
	url: "members.xml",
	dataType: "XML",
	success: function(members){
		$(members).find('member').each(function(index,memberDetails){
			console.log($(memberDetails).find('lastName').text());
			console.log($(memberDetails).find('firstName').text());
			console.log($(memberDetails).find('age').text());
			console.log($(memberDetails).find('sex').text());
			console.log($(memberDetails).find('location').text());
			console.log("\n");
		});
	}
});

//result
/*
Jackson
Barbara
27
F
Florida

Kimball
Andrew
25
M
Texas

Baker
John
28
M
Arkansas

Gamble
Edward
29
M
Virginia

Anderson
Kimberly
23
F
Tennessee

Houston
Franchine
25
F
Idaho

Franklin
Howard
24
M
California

Chen
Dan
26
M
Washington

Daniel
Carolyn
27
F
North Carolina

Englert
Grant
25
M
Delaware
*/

 

Related Tutorials


Must Read