码迷,mamicode.com
首页 > Web开发 > 详细

轻松使用jquery解析XML

时间:2014-09-05 12:42:11      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   使用   java   ar   

xml文件结构:books.xml

bubuko.com,布布扣<?xml version="1.0" encoding="UTF-8"?>
bubuko.com,布布扣<root>
bubuko.com,布布扣    <book id="1">
bubuko.com,布布扣        <name>深入浅出extjs</name>
bubuko.com,布布扣        <author>张三</author>
bubuko.com,布布扣        <price>88</price>
bubuko.com,布布扣    </book>
bubuko.com,布布扣    <book id="2">
bubuko.com,布布扣        <name>锋利的jQuery</name>
bubuko.com,布布扣        <author>李四</author>
bubuko.com,布布扣        <price>99</price>
bubuko.com,布布扣    </book>
bubuko.com,布布扣    <book id="3">
bubuko.com,布布扣        <name>深入浅出flex</name>
bubuko.com,布布扣        <author>王五</author>
bubuko.com,布布扣        <price>108</price>
bubuko.com,布布扣    </book>
bubuko.com,布布扣    <book id="4">
bubuko.com,布布扣        <name>java编程思想</name>
bubuko.com,布布扣        <author>钱七</author>
bubuko.com,布布扣        <price>128</price>
bubuko.com,布布扣    </book>
bubuko.com,布布扣</root>
bubuko.com,布布扣

页面代码:

bubuko.com,布布扣<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
bubuko.com,布布扣<html>
bubuko.com,布布扣<head>
bubuko.com,布布扣<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
bubuko.com,布布扣<title>jquery解析xml</title>
bubuko.com,布布扣<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
bubuko.com,布布扣<script type="text/javascript">
bubuko.com,布布扣    $(function(){
bubuko.com,布布扣        $.post(‘books.xml‘,function(data){
bubuko.com,布布扣            //查找所有的book节点
bubuko.com,布布扣            var s="";
bubuko.com,布布扣            $(data).find(‘book‘).each(function(i){
bubuko.com,布布扣                var id=$(this).attr(‘id‘);
bubuko.com,布布扣                var name=$(this).children(‘name‘).text();
bubuko.com,布布扣                var author=$(this).children(‘author‘).text();
bubuko.com,布布扣                var price=$(this).children(‘price‘).text();
bubuko.com,布布扣                s+=id+"&nbsp;&nbsp;&nbsp;&nbsp;"+name+"&nbsp;&nbsp;&nbsp;&nbsp;"+author+"&nbsp;&nbsp;&nbsp;&nbsp;"+price+"<br>";
bubuko.com,布布扣            });
bubuko.com,布布扣            $(‘#mydiv‘).html(s);
bubuko.com,布布扣        });
bubuko.com,布布扣    });
bubuko.com,布布扣</script>
bubuko.com,布布扣</head>
bubuko.com,布布扣<body>
bubuko.com,布布扣    <div id=‘mydiv‘></div>
bubuko.com,布布扣</body>
bubuko.com,布布扣</html>

效果图:
bubuko.com,布布扣 
完整示例下载

轻松使用jquery解析XML

标签:style   blog   http   color   os   io   使用   java   ar   

原文地址:http://www.cnblogs.com/xiaochao12345/p/3957757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!