jQuery解析xml
2011-08-15 00:20
232 查看
books.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<book id="1">
<name>深入浅出extjs</name>
<author>张三</author>
<price>88</price>
</book>
<book id="2">
<name>锋利的jQuery</name>
<author>李四</author>
<price>99</price>
</book>
<book id="3">
<name>深入浅出flex</name>
<author>王五</author>
<price>108</price>
</book>
<book id="4">
<name>java编程思想</name>
<author>钱七</author>
<price>128</price>
</book>
</root>
demo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jquery解析xml</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
/*
$(function(){
$.post('books.xml',function(data){
//查找所有的book节点
var s="";
$(data).find('book').each(function(i){
var id=$(this).attr('id');
var name=$(this).children('name').text();
var author=$(this).children('author').text();
var price=$(this).children('price').text();
s+=id+" "+name+" "+author+" "+price+"<br>";
});
$('#mydiv').html(s);
});
});
*/
$(document).ready(function(){
$.ajax({
url:"books.xml",
type:"POST",
dataType:($.browser.msie) ? "text" : "xml",
success:function(data){
var xml;
if( typeof data == "string" ){
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
}
var html="";
$(xml).find('book').each(function(i){
id=$(this).attr('id');
name=$(this).children('name').text();
author=$(this).children('author').text();
price=$(this).children('price').text();
html=html+"<tr><td>"+id+"</td><td>"+name+"</td><td>"+author+"</td><td>"+price+"</td></tr>";
/*
alert("id:"+id+
" name:"+name+
" author:"+author+
" price:"+price
);
*/
});
//$("#dataTab").html(html);
$("#dataTab").append(html);
},
error:function(data){
alert("解析失败");
}
});
});
var id="";
var name="";
var author="";
var price="";
</script>
</head>
<body>
<table name="dt" id="dataTab" >
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<book id="1">
<name>深入浅出extjs</name>
<author>张三</author>
<price>88</price>
</book>
<book id="2">
<name>锋利的jQuery</name>
<author>李四</author>
<price>99</price>
</book>
<book id="3">
<name>深入浅出flex</name>
<author>王五</author>
<price>108</price>
</book>
<book id="4">
<name>java编程思想</name>
<author>钱七</author>
<price>128</price>
</book>
</root>
demo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jquery解析xml</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
/*
$(function(){
$.post('books.xml',function(data){
//查找所有的book节点
var s="";
$(data).find('book').each(function(i){
var id=$(this).attr('id');
var name=$(this).children('name').text();
var author=$(this).children('author').text();
var price=$(this).children('price').text();
s+=id+" "+name+" "+author+" "+price+"<br>";
});
$('#mydiv').html(s);
});
});
*/
$(document).ready(function(){
$.ajax({
url:"books.xml",
type:"POST",
dataType:($.browser.msie) ? "text" : "xml",
success:function(data){
var xml;
if( typeof data == "string" ){
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
}
var html="";
$(xml).find('book').each(function(i){
id=$(this).attr('id');
name=$(this).children('name').text();
author=$(this).children('author').text();
price=$(this).children('price').text();
html=html+"<tr><td>"+id+"</td><td>"+name+"</td><td>"+author+"</td><td>"+price+"</td></tr>";
/*
alert("id:"+id+
" name:"+name+
" author:"+author+
" price:"+price
);
*/
});
//$("#dataTab").html(html);
$("#dataTab").append(html);
},
error:function(data){
alert("解析失败");
}
});
});
var id="";
var name="";
var author="";
var price="";
</script>
</head>
<body>
<table name="dt" id="dataTab" >
</table>
</body>
</html>
相关文章推荐
- jquery解析XML及获取XML节点名称
- JQuery解析xml示例
- JQUERY解析XML
- jquery加载解析XML文件
- JQuery解析XML数据的几个例子
- JQuery解析XML数据的几个例子
- jquery解析XML字符串和XML文件的方法说明
- 160928、JQuery解析XML数据的demo
- JQuery解析xml文件
- jQuery使用ajax传输xml文档在IE中无法正确解析的问题
- 基于jQuery解析XML的插件
- jquery解析XML通用方法学习示例
- JQuery 解析xml
- 使用jquery解析XML示例代码
- ie中jquery无法解析XML解决
- jquery解析xml的例子
- jquery解析XML
- jQuery1.6解析XML文件
- JQuery解析xml