源于生活

标题: 通过jquery读取xml并形成一个表格 [打印本页]

作者: kobiny    时间: 2017-1-24 11:10
标题: 通过jquery读取xml并形成一个表格
stu.xml
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="ISO-8895-1" ?>
<message>
<student>
<name>NAME</name>
<num>NUM</num>
<sex>SEX</sex>
<age>AGE</age>
<dept>DEPARTMENT</dept>
</student>
<student>
<name>jack</name>
<num>2012001</num>
<sex>man</sex>
<age>20</age>
<dept>English</dept>
</student>
<student>
<name>John</name>
<num>2012002</num>
<sex>man</sex>
<age>21</age>
<dept>Chinese</dept>
</student>
<student>
<name>Max</name>
<num>2012003</num>
<sex>woman</sex>
<age>19</age>
<dept>Cumputer</dept>
</student>
<student>
<name>Kitty</name>
<num>2012004</num>
<sex>woman</sex>
<age>20</age>
<dept>Math</dept>
</student>
</message>



stuMsg.html
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script>
$.ajax({
    type:"get",
    dataType:"binary",
    url:"stu.xml",
    success:function(xmlData){
        var htmlData="<table border='1'>";
        $(xmlData).find('student').each(function(){
        var $student = $(this);  
        var name = $student.find('name').text();
        var num = $student.find('num').text();
        var sex = $student.find('sex').text();
        var age = $student.find('age').text();
        var dept = $student.find('dept').text();
        htmlData += '<tr>'+'<td>'+name+'</td>'+'<td>'+num+'</td>'+'<td>'+sex+'</td>'+'<td>'+age+'</td>'+'<td>'+dept+'</td>'+'</tr>';  
        });
        htmlData+="</table>";
        $("body").append(htmlData);
    },
    error:function(xhr, status, error){
        alert(status)
    }
})
</script>
</head>
<body>
</body>
</html>








欢迎光临 源于生活 (http://bbs.vingoo.info/) Powered by Discuz! X3.1