範例code
1. XML
<?xml version="1.0" encoding="utf-8" ?>
<RecentBooks>
<Book>
<Title>My Cool Book Title</Title>
<Description>The my cool book is possibly the best cool book in that any developer could use to be a great web designer.</Description>
<Date>12/1/2010</Date>
</Book>
<Book>
<Title>Another PHP book</Title>
<Description>Learn everything about PHP with 'Another PHP book,' your ultimate guide to the ins and outs of PHP.</Description>
<Date>4/1/2010</Date>
</Book>
<Book>
<Title>jQuery Techniques</Title>
<Description>jQuery techniques runs you through real life examples of jQuery from beginner to expert</Description>
<Date>6/2/2010</Date>
</Book>
<Book>
<Title>MySQL Database Book</Title>
<Description>Brush up your knowledge with the best MySQL database book on the market.</Description>
<Date>14/2/2010</Date>
</Book>
</RecentBooks>
2. JQuery Code
$(document).ready(function () {
$.ajax({
type: "GET",
url: "books.xml",
dataType: "xml",
success: xmlParser
});
});
function xmlParser(xml) {
$('#load').fadeOut();
$(xml).find("Book").each(function () {
$(".main").append('<div class="book"><div class="title">' + $(this).find("Title").text() + '</div><div class="description">' + $(this).find("Description").text() + '</div><div class="date">Published ' + $(this).find("Date").text() + '</div></div>');
$(".book").fadeIn(1000);
});
}
3. 網頁上 加一個 div tag 名為 'main'
不過值得注意的是 XML 只限讀取同一網域的文件, 若要使用這種功能的話只能用JSON的方式
參考網頁: http://papermashup.com/parse-xml-with-jquery/
沒有留言:
張貼留言