How to parse xml using jquery

Index.html:

<html>

<head>
	
</head>
<body>
<div id="content"></div>
 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript">
$(document).ready(function(){
 
 
function parse(document){
  $(document).find("book").each(function(){
    $("#content").append(
	'<p>ID: '+$(this).attr('id')+
	'<br /> Title: '+$(this).find('title').text()+
	'<br /> Author: '+$(this).find('author').text()+
	'</p>'
	);
  });
}
 
 
 
  $.ajax({
    url: 'read.xml', // name of file you want to parse
    dataType: "xml",
    success: parse,
	error: function(){alert("Error: Something went wrong");}
  });
});
</script>
</body>
</html>

read.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>

<library>
	<book id="5412" num="19191919">
		<title>Java Script</title>
		<author fname="abc" lname="xyz">Abc Xyz</author>
		<publisher>Sky Publishers</publisher>
		<price>545 rupees</price>
	</book>
	<book id="2893" num="3636363">
		<title>HTML5</title>
		<author fname="indian" lname="john">Indian John</author>
		<publisher>Sky Publishers</publisher>
		<price>855 rupees</price>
	</book>
</library>

Crap data: Please not to look into this..i have been trying in different ways..the below code is just a crap. pls ignore.thanks

// $(document).find(“seat”).each(function () {
/* $(“#content”).append(
‘<p>Num: ‘+$(this).attr(‘num’)+
‘<br /> Game: ‘+$(this).find(‘game’).attr(‘no’)+
‘ Result: ‘+$(this).find(‘game’).attr(‘result’)+
‘</p>’
);*/
var $kids = $(obj).find(“BJ_BetBehindHistory”).children(“seat”);
// seat num and id is displayed here
for (i = 0; i < $kids.length; i++) {
var foo = $kids[i];
//for (var j = 0; j < foo.attributes.length; j++) {
var attr = foo.attributes[0];
console.log(attr.name + ” = ” + attr.value);
seatnum = attr.value;

if (seatnum == -1) {
var dealer = $kids[i];
// var test = dealer.children(“game”)
console.log($(obj).find((“seat”).attr(‘num’) == seatnum));
console.log(dealer);
}
// game no and result code is displayed
/* var $game = $(document).find(“seat”).children(“game”);
for (k = 0; k < $game.length; k++) {
var boom = $game[k];
for (var l = 0; l < boom.attributes.length; l++) {
var attr = boom.attributes[l];
console.log(attr.name + ” = ” + attr.value);
}

}*/

// end of game no and result displat
//}
//console.log(foo);
function parse(obj) {
console.log(obj);
console.log($(“seat[num=’0′]”,obj));

}

$(“seat[num=’0′]”, obj).each(function (ind, val) {
console.log(ind, val);

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s