html5怎么通过javascript读取,存取xml?

html5怎么通过javascript读取,存取xml?,第1张

function LoadXMLFile(xmlFile) {

var xmlDom = null

if (window.ActiveXObject) {

xmlDom = new ActiveXObject("Microsoft.XMLDOM")

//xmlDom.loadXML(xmlFile)//如果用的是XML字符串

xmlDom.load(xmlFile)//如果用的是xml文件。

} else if (document.implementation &&document.implementation.createDocument) {

var xmlhttp = new window.XMLHttpRequest()

xmlhttp.open("GET", xmlFile, false)

xmlhttp.send(null)

xmlDom = xmlhttp.responseXML.documentElement//一定要有根节点(否则google浏览器读取不了)

} else {

xmlDom = null

}

return xmlDom

}

html代码如下:

<html>

<head>

<title>html读取xml数据实例一</title>

</head>

<body>

<xml id="xx" src="table.xml">

</xml>

<h3>html读取xml数据实例一</h3>

<table datasrc="#xx" border="1">

<thead> <th>序号</th> <th>区域</th> <th>名字</th> <th>电话</th> <th>QQ</th> <th>email</th>

</thead>

<tr>

<td><span datafld="nid"></span></td>

<td><span datafld="area"></span></td>

<td><span datafld="salename"></span></td>

<td><span datafld="phone"></span></td>

<td><span datafld="qq"></span></td>

<td><span datafld="email"></span></td>

</tr>

</table>

</body>

</html>

xml文件如下:

<?xml version="1.0"?>

<!DOCTYPE tables[

<!ELEMENT tables (table)*>

<!ELEMENT table(nid, area, salename, phone,qq,email)>

<!ELEMENT nid (#PCDATA)>

<!ELEMENT area (#PCDATA)>

<!ELEMENT salename (#PCDATA)>

<!ELEMENT phone (#PCDATA)>

<!ELEMENT qq (#PCDATA)>

<!ELEMENT email (#PCDATA)>

]>

<tables>

<table>

<nid>1</nid>

<area>广东深圳</area>

<salename>张XX</salename>

<phone>0755-265XXXX1,1382XXX409</phone>

<qq>12XXX88</qq>

<email>TSXXX@c-sct.com</email>

</table>

<table>

<nid>1</nid>

<area>广东深圳</area>

<salename>张XX</salename>

<phone>0755-265XXXX1,1382XXX409</phone>

<qq>12XXX88</qq>

<email>TSXXX@c-sct.com</email>

</table>

<table>

<nid>1</nid>

<area>广东深圳</area>

<salename>张XX</salename>

<phone>0755-265XXXX1,1382XXX409</phone>

<qq>12XXX88</qq>

<email>TSXXX@c-sct.com</email>

</table>

</tables>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7312066.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存