2016-10-13 00:16:48 点击量:52 标签: 收藏本文
[JS]用js来读取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>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
- <title>js读取xml</title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
- type="text/javascript"></script>
- <script type="text/javascript">
- //酷播cuplayer.com提示:使用jquery框架事半功倍
- $(document).ready(function(){
- var imageID;
- var xml;
- loadXML("test.xml");
- $("#imgInfo").find("img").mouseover(function(){
- //酷播cuplayer.com提示:鼠标移入imgInfo里的IMG
- imageID=$(this).attr("id");
- //酷播cuplayer.com提示:获取移入图片的ID
- $(xml).find("Image").each(function(){
- //遍历xml的image标签
- if($(this).attr("ID")==imageID)
- //相等设置输出
- $("#output").html("ID:"+$(this).attr("ID")+",TT:"+$(this).attr("TT")+",Time:"+$(this).attr("Time"))
- })
- })
- $("#imgInfo").find("img").mouseout(function(){//鼠标移出
- $("#output").html("")//清空
- )}
- function loadXML(fileName) {//ajax方式读取xml
- $.ajax({
- type: "GET",
- url: fileName,
- dataType: ($.browser.msie) ? "text" : "xml",
- success: function(data, textStatus, jqXHR){//读取成功
- if (typeof data == "string") {
- xml = new ActiveXObject("Microsoft.XMLDOM");
- xml.async = false;
- xml.loadXML(data);
- } else {
- xml = data;
- }
- },
- error: function(jqXHR, textStatus, errorThrown) {//读取失败时
- _defaultAjaxError(jqXHR, textStatus, errorThrown);
- }
- });
- }
- function _defaultAjaxError(jqXHR, textStatus, errorThrown) {
- alert(jqXHR.status + ' * ' + jqXHR.statusText + ' * ' + jqXHR.responseText );
- alert(textStatus);
- }
- });
- </script>
- </head>
- <body>
- <div id="imgInfo">
- <image src="player_img_path/20161202/udjctmbptx1.jpg" ID="566" />
- <image src="player_img_path/20161202/fhczl2dbszf.jpg" ID="786" />
- </div>
- <div id="output"style="color:red">酷播cuplayer.com提示:输出信息</div>
- </body>
- </html>
[JS]用js来读取xml文件的信息
xml内容信息:
- <?xml version="1.0" encoding="UTF-8"?>
- <root>
- <Image TT="pic1" Time="546754" ID="566"/>
- <Image TT="pic2" Time="678844" ID="786"/>
- </root>
出处http://blog.csdn.net/yukon12345
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室