jQuery、JS读取xml文件里的内容(JS先通过document.implementation.createDocument方法将xml转换成document对象,jQuery将读取到的xml转成table)
xml文件:test.xml

<?xml version="1.0"?> <note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
<lastname>Simth</lastname> </note>

html文件:
1.先通过document.implementation.createDocument方法将xml转换成document对象
2.在通过js获取document中的元素并其值

<html>
<head>
<script type="text/javascript">
function parseXML(){
try{
xmlDoc= new ActiveXObject("Microsoft.XMLDOM");
}catch(e){
try{
xmlDoc= document.implementation.createDocument("","",null);
}catch(e){
alert(e.message);
return;
}
}
xmlDoc.async = false;
xmlDoc.load("../xml/test.xml");
document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}
</script>
</head>
<body onload="parseXML()">
<h1>W3School.com.cn Internal Note</h1>
<p><b>To:</b><span id="to"></span><br/>
<b>From:</b><span id="from"></span><br/>
<b>Message:</b><span id="message"></span>
<p>
</body>
</html>
=====================JS读取xml文件,并且转成table表格展示==================
先看xml文件:

<?xml version="1.0" standalone="yes"?>
<student>
<stuinfo>
<stuName>张秋丽</stuName>
<stuSex>女 </stuSex>
<stuAge>18</stuAge>
</stuinfo>
<stuinfo>
<stuName>李文才</stuName>
<stuSex>男 </stuSex>
<stuAge>31</stuAge>
</stuinfo>
<stuinfo>
<stuName>李斯文</stuName>
<stuSex>男 </stuSex>
<stuAge>22</stuAge>
</stuinfo>
<stuinfo>
<stuName>马英</stuName>
<stuSex>女 </stuSex>
<stuAge>25</stuAge>
</stuinfo>
<stuinfo>
<stuName>孙红雷</stuName>
<stuSex>男 </stuSex>
<stuAge>32</stuAge>
</stuinfo>
<stuinfo>
<stuName>欧阳俊雄</stuName>
<stuSex>男 </stuSex>
<stuAge>28</stuAge>
</stuinfo>
<stuinfo>
<stuName>江琳</stuName>
<stuSex>女 </stuSex>
<stuAge>23</stuAge>
</stuinfo>
<stuinfo>
<stuName>小小</stuName>
<stuSex>女 </stuSex>
<stuAge>22</stuAge>
</stuinfo>
</student>

aspx页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="获取数据库数据生成XML.aspx.cs" Inherits="Chapter1.获取数据库数据生成XML" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function loadXMLDoc(dname) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
}
else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", dname, false);
xhttp.send("");
return xhttp.responseXML;
}
function ReadXml() {
var xmldoc = loadXMLDoc("Student.xml");
//获得指定节点
var divmsg = document.getElementById("xmlMsg");
var msg = "<table border='1' id='mytable'><tr><th>姓名</th><th>性别</th><th>年龄</th><tr>";
var nodes = xmldoc.getElementsByTagName("stuinfo");
for (var i = 0; i < nodes.length; i++) {
msg += "<tr>";
msg += "<td>" + nodes[i].getElementsByTagName("stuName")[0].firstChild.nodeValue + "</td>";
msg += "<td>" + nodes[i].getElementsByTagName("stuSex")[0].firstChild.nodeValue + "</td>";
msg += "<td>" + nodes[i].getElementsByTagName("stuAge")[0].firstChild.nodeValue + "</td>";
msg += "</tr>";
}
msg += "</table>";
divmsg.innerHTML = msg;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="JS读取XML" onclick="ReadXml()" /><br />
<div id="xmlMsg">
</div>
</div>
</form>
</body>
</html>

上面的JS操作主要就避免了使用childNodes(因为火狐中有时候会出现childNodes[0]获取到的是"\n"而不是我们想要的第一个子节点,这个自己可以去试下,反正我是遇到了这种情况),使得可以兼容IE、火狐,其他浏览器我没试。
===========jQuery将读到的xml文件转成了table表格展示========================
地址:https://zhidao.baidu.com/question/581652437.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="<head><title>qqqun21/777/12</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"/><script src="jquery-1.8.0.js" type="text/javascript"></script><script>window.onload = function(){ $("div").load("x.xml",function(response,status,xhr){ if(status=="success"){ $("div").html("<table border=1></table>"); $(response).find("TestCase").each(function(){ var name = $(this).children("Name").text(); var testTime = $(this).children("TestTime").text(); var result = $(this).children("Result").text(); var co = result=="False"?"red":"#fff"; var html = "<tr style='Courier New", monospace; border-radius: 0px !important; background: none !important; border: 0px !important; bottom: auto !important; float: none !important; left: auto !important; line-height: 1.1em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; margin: 0px !important; padding: 0px !important; color: rgb(0, 0, 0) !important;">><td>"+name +"</td><td>"+testTime+"</td><td>"+result+"</td></tr>"; $("table").append(html); }); }else{ $("div").html("error occured:"+xhr.status+" "+xhr.statusText); } })};</script></head><body><div></div></body>jQuery、JS读取xml文件里的内容(JS先通过document.implementation.createDocument方法将xml转换成document对象,jQuery将读取到的xml转成table)的更多相关文章
- Flex读取txt文件里的内容(二)
Flex读取txt文件里的内容 自己主动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8&quo ...
- Flex读取txt文件里的内容(一)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/you23hai45/article/details/25248307 Flex读取txt文件里的内 ...
- Flex读取txt文件里的内容报错
Flex读取txt文件里的内容 1.详细错误例如以下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativeP ...
- 曹工说Spring Boot源码(15)-- Spring从xml文件里到底得到了什么(context:load-time-weaver 完整解析)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- Flex读取txt文件中的内容(三)
Flex读取txt文件中的内容 1.设计源码 LoadTxt.mxml: <?xml version="1.0" encoding="utf-8"?> ...
- Flex读取txt文件中的内容(二)
Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" ...
- 不在JPA 的 persistence.xml 文件里配置Entity class的解决的方法
在Spring 集成 Hibernate 的JPA方式中,须要在persistence配置文件里定义每个实体类.这样很地不方便.2种方法能够解决此问题: 这2种方式都能够实现不用在persist ...
随机推荐
- kafka代码测试连接
1.发送: package kafka.test; import java.util.Date;import java.util.Properties;import java.util.Random; ...
- python学习之内存驻留机制简述
第四章 4.1 小数据池 4.1.1 代码块 一个模块,一个函数,一个类,甚至一个command命名都可以称之为一个代码块. 官方解释: A Python program is constructed ...
- PHP操作redis部分命令
//连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('12345 ...
- [JavaScript] console.log只在查看时才会读取这个打印的对象,并把此刻相关属性和值显示出来
/** * 写个函数解决console.log只在查看时才会读取这个打印的对象,并把此刻相关属性和值显示出来 * @param arg */ const log = function (...ar ...
- DataGridViewCheckBoxColumn的Value值和EditFormatedValue值不一致
今天要做一个代码修改DataGridViewCheckBoxColumn的Value值然后再遍历获取DataGridview选中项,因为遍历的时候为了能获取跟界面一致的选项,所以判断是否选中使用的是E ...
- 2019CSP-S游记(真)
本来是考完了的,但是由于江西省的负责人员的不小心(?),江西oier的大部分代码都被删掉了, 所以我们需要重考,想看我之前CSP的游记可以看这个点我.下面是我江西重考的游记: Day0 又集训了一个星 ...
- BZOJ 1053 反素数 题解
题面 引理1: 1~n中的最大反质数,就是1~n中约数个数最多的数中最小的一个(因为要严格保证g(x)>g(i)): 引理2:1~n中任何数的不同因子不会超过10个,因为他们的乘积大于2,00 ...
- HDU-4507-吉哥系列故事-恨7不成妻
题目描述 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 ...
- C++中多态的概念和意义
1,函数重写回顾: 1,父类中被重写的函数依然会继承给子类: 2,子类中重写的函数将覆盖父类中的函数: 1,重写父类当中提供的函数是因为父类当中提供的这个函数版本不能满足我们的需求,因此我们要重写: ...
- yii框架製作簡易RBAC權限管理
控制器源碼 <?php namespace app\controllers; use yii; use yii\web\Controller; class PowerController ext ...