-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery-xml-parser-example.html
92 lines (91 loc) · 3.64 KB
/
jquery-xml-parser-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE HTML>
<html>
<head>
<title>jQuery based XML parser</title>
<style>
body{
color:#222;
font-size: 12px;
font-family: sans-serif;
}
#feedheader{
margin:0;
padding:0;
font-size:18px;
}
.feedlink{
text-decoration: none;
}
.feeditem{
padding: 3px;
margin: 3px 0;
background-color: #eee;
}
a.feeditemlink{
text-decoration: none;
color: #222;
}
a.feeditemlink:hover{
background-color: #ddd;
}
</style>
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-1.6.4.js"></script>
<script type="text/javascript" charset="utf-8">
$( function() {
var XMLURI = 'xml-feed.xml';
$.ajax({
url: XMLURI,
success: function(xml){
var feedtitle = $(xml).find('channel title:first'), //find first tag or node in XML that is named title
feedlink = $(xml).find('channel link:first'), //find first tag or node in XML that is named link
feedtitle = '<a href="' + feedlink.text() +'" class="feedlink">' + feedtitle.text() + '</a>',
feedItem = '';
//The xpath way
$(xml).find( "channel>item" ).each(
function(){
var item = $(this),
title = item.find('title').text(),
link = item.find('link').text(),
itemid = item.attr('id');//get an attribute
console.log(title);
console.log(link);
console.log('The attribute value "' + itemid + '".');
}
);
//The other way around.
$(xml).find( "item" ).each(
function(){
var item = $(this),
title = item.find('title').text(),
link = item.find('link').text(),
itemid = item.attr('id');//get an attribute
//you can log all attributes
//console.log(item[0].attributes);
feedItem = feedItem + '<div class="feeditem">';
feedItem = feedItem + '<a href="' + link + '" class="feeditemlink">';
feedItem = feedItem + '<span class="feeditemtitle">' + title+ '</span>';
feedItem = feedItem + '</a>';
feedItem = feedItem + ' id = ' + itemid;
feedItem = feedItem + '</div>';
}
);
$('#feedheader').html(feedtitle);
$('#content').append(feedItem);
},
error: function(){
$('#feedheader').("PC LOAD LETTER - #http://www.youtube.com/watch?feature=player_detailpage&v=WUUptX0i55g#t=43s");
},
dataType: "xml"
});
});
</script>
</head>
<body>
<div data-role="page" id="home">
<div data-role="header">
<h1 id="feedheader">Loading...</h1>
</div>
<div data-role="content" id="content"> </div>
</div>
</body>
</html>