js 如何读取xml文档内容

js 如何读取xml文档内容,第1张

请把下面的代码保存为 readxml.html

<html>

<head>

<script type="text/javascript">

var xmlhttp

function loadXMLDoc(url)

{

xmlhttp=null

if (window.XMLHttpRequest)

  {// code for IE7, Firefox, Opera, etc.

  xmlhttp=new XMLHttpRequest()

  }

else if (window.ActiveXObject)

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

  }

if (xmlhttp!=null)

  {

  xmlhttp.onreadystatechange=state_Change

  xmlhttp.open("GET",url,true)

  xmlhttp.send(null)

  }

else

  {

  alert("Your browser does not support XMLHTTP.")

  }

}

function state_Change()

{

if (xmlhttp.readyState==4)

  {// 4 = "loaded"

  if (xmlhttp.status==200)

    {// 200 = "OK"

    document.getElementById('A1').innerHTML=xmlhttp.status

    document.getElementById('A2').innerHTML=xmlhttp.statusText

    

    document.getElementById('A3').innerHTML=xmlhttp.responseText

    }

  else

    {

    alert("Problem retrieving XML data:" + xmlhttp.statusText)

    }

  }

}

</script>

</head>

<body>

<h2>Using the HttpRequest Object</h2>

<p><b>Status:</b>

<span id="A1"></span>

</p>

<p><b>Status text:</b>

<span id="A2"></span>

</p>

<p><b>Response:</b>

<br /><span id="A3"></span>

</p>

<button onclick="loadXMLDoc('note.xml')">Get XML</button>

</body>

</html>

请把下面的文件保存未  note.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<note>

<to>George</to>

<from>John</from>

<heading>Reminder</heading>

<body>Don't forget the meeting!</body>

</note>

用xslt比较简单,而且速度也会比javascript遍历dom快很多。

function transformToTable(xmlUrl,xslUrl,appendTo){

var xmldom=document.implementation.createDocument()

xmldom.load(xmlUrl)

var xsldom=document.implementation.createDocument()

xsldom.load(xslUrl)

var xslProcessor=new XSLTProcessor()

xslProcessor.importStylesheet(xsldom)

var result=xslProcessor.transformToTragment(xmldom,document)

appendTo.appendChild(result)

}

只是简单写了写,提供大致思路

xsl和xslt的教程可以看http://www.w3course.net/xsl/index.htm

要把html的表格改成xml的可以用正则表达式去匹配innerHTML,这种方法当然是比较快的

还有就是遍历,比较麻烦,而且速度也会慢一些

function transformToXML(elem){

if (typeof elem=='string'){

var div=document.createElement('DIV')

div.style.display='none'

div.innerHTML=elem

document.body.appendChild(div)

elem=document.body.lastChild.getElementsByTagName('table')[0]

}

if (elem.nodeName!='TABLE') return

var result='<?xml version="1.0" encoding="utf-8" standalone="yes"?>'+"\n"

var titles=[],tr,i,j

result+='<booklist>'+"\n"

tr=elem.getElementsByTagName('tr')

if (tr[0].firstChild.nodeName=='th'){

for (i=0i<tr[0].childNodes.lengthi++){

if (tr[0].childNodes[i].nodeType==1){

titles.push(tr[0].childNodes[i].innerHTML.replace(/(^\s|\s$)/,''))

}

}

}

for(i=0i<tr.lengthi++){

result+='<book>'+"\n"

for(j=0j<tr[i].childNodes.lengthj++){

if (tr[i].childNodes[j].nodeType!=1) continue

if (!titles[j]){

titles[j]='item'

}

result+='<'+titles[j]+'>'

+tr[i].childNodes[j].innerHTML

+'</'+titles[j]+'>'+"\n"

}

result+='</book>'+"\n"

}

result+='</booklist>'+"\n"

return result

}

上面是一个遍历的函数,虽然还不完善,但是已经满足基本的功能了。可以在firebug里测试一下

console.log(transformToXML('<table><tr><td>some book</td></tr></table>'))

函数里面booklist和book之类的标签名

下面是一个正则表达式的函数

function transformToXML(elem){

if (typeof elem=='object'){

elem='<table>'+elem.innerHTML+'</table>'

}

return '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'+

elem.replace(/<table(\s*)[^>]+>/,'<booklist>')

.replace(/<\/table>/,'</booklist>')

.replace(/<(\/?)(tbody|tfoot|thead)(\s*)[^>]+>/,'')

.replace(/<tr(\s*)[^>]+>/,'<book>')

.replace(/<\/tr>/,'</book>')

.replace(/<td(\s*)[^>]+>/,'<item>')

.replace(/<\/td>/,'</item>')

}


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

原文地址:https://54852.com/tougao/11828473.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存