请把下面的代码保存为 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>
JS]利用js将读取到的xml文件中的数据显示到html文档上script type="text/javascript" language="javascript">
var xmlDoc = checkXMLDocObj('../openClass.xml')//读取到xml文件中的数据
var a = document.getElementsByTagName("a")//获取所有的A标签
$(document).ready(function () {
var nodes
if($.browser.msie){ // 注意各个浏览器之间的区别
nodes = xmlDoc.getElementsByTagName('collage')[0].childNodes//读取XML文件中需要显示的数据
}
else if (isFirefox=navigator.userAgent.indexOf("Firefox")>0){
nodes = xmlDoc.getElementsByTagName('collage')[0].children//读取XML文件中需要显示的数据
}
else{
nodes = xmlDoc.getElementsByTagName('resource')
}
for (var i = 0i <a.lengthi++) {
if (a[i].parentNode.nodeName == "SPAN") {
for (var j = 0j <nodes.lengthj++) {
var resource = nodes[j]
var url = resource.getAttribute('url')
var href=$(a[i]).attr("href")
if (href == url) {
var count = resource.getAttribute('click')
var span = document.createElement("div")
var str = document.createTextNode("点击率:" + count)
span.appendChild(str)
var div = a[i].parentNode.parentNode
div.appendChild(span)
break
}
}
}
}
})
$(function(){ //通过get请求,将点击率增加
$(a).mousedown(function(){
var href = $(this).attr("href")
$.get("../receive.ashx",{url:href,rd:Math.random()}, function (msg) {
})
})
})
</script>
var xmlStr = '<root><people><name>张三</name><address>北京</address></people></root>'var result = null
eval('result=['+$(xmlStr).find('people').map(function(){
return '{'+$('*',this).map(function(){
return $(this).attr('tagName') + ':"' + $(this).text() + '"'
}).get().join(',') + '}'
}).get().join('],[')+']')
alert(result)
//result是一个数组,如果有多个people节点,则为:[{name:"张三",address="北京"},{name:"李四",address="上海"}]
不好意思偏题了,以下是你要的结果:
var result = $(xmlStr).find('people').children().map(function(){
return $(this).attr('tagName')+'='+$(this).text()
}).get().join()
上面的都是jquery的写法:
javascript的话:
用COM操作:
object xmlDoc = new ActiveObject('msxml2')
xmlDoc.loadXml(xmlStr)
var pNode = xmlDoc.selectSignNode('people')
var result = ''
for(var node in pNode.selectNodes('*')){
result+=(result.length>0?',':'') + node.nodeName+'='+node.innerText
}
alert(reuslt)
或者用DOM操作:
<xml id='testXml' />
var result = ''
with(document.getElementById('testXml')){
innerHTML = xmlStr
for(var child in children){
result += ',' + child.tagName + '=' + child.innerText
}
if(result.length >0){
result = result.substring(1)
}
}