var xmldom
function test()
{
xmldom = new ActiveXObject("Msxml2.DOMDocument")
xmldom.async = false
xmldom = document.all.mxh.XMLDocument
}
function test2()
{
var xmldom = new ActiveXObject("Msxml2.DOMDocument")
xmldom.async = false
xmldom = document.all.mxh.XMLDocument
node = xmldom.selectSingleNode("//nodeB")
node2 = xmldom.createElement("nodeB")
node2.text=parseInt(node.text)+1
xmldom.documentElement.replaceChild(node2,node)
node = xmldom.selectSingleNode("//nodeB")
alert(node.text)
alert(xmldom.xml)
}
function test3()
{
document.f.xmldata.value= document.all.mxh.XMLDocument.xml
alert(document.f.xmldata.value)
document.f.submit()
}
</script>
<body>
<XML id=mxh>
<root>
<nodeA>nodeA</nodeA>
<nodeB>30</nodeB>
</root>
</XML>
<form name=f action="xx.asp">
<input name=xmldata type=hidden>
<input onclick="test()" type="button" value=old>
<input onclick="test2()" type="button" value=请多点几次>
<input onclick="test3()" type="button" value=保存>
</form>
保存,调用 xmldom.save("xxx.xml")
请把下面的代码保存为 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>
///////////////////////////////////////////////////////////首先:xml文件(tree.xml)内容如下:
<?xml version="1.0" encoding="gb2312"?>
<treeview>
<tree id="p1">
<text>山东省</text>
<target>_blank</target>
<title>省份</title>
<link></link>
<tree id="p1-1">
<text>威海市</text>
<target>_blank</target>
<title>城市</title>
<link></link>
</tree>
<tree id="p1-2">
<text>烟台市</text>
<target>_blank</target>
<title>城市</title>
<link></link>
<node id="p1-2-1">
<text>长夼村</text>
<target>_blank</target>
<title>乡镇</title>
<link>http://www.baidu.com/</link>
</node>
</tree>
<node id="p1-3">
<text>富镇</text>
<target>_blank</target>
<title>乡镇</title>
<link>http://www.baidu.com/</link>
</node>
</tree>
<tree id="p2">
<text>河北省</text>
<target>_blank</target>
<title>省份</title>
<link></link>
<tree id="p2-1">
<text>泊头市</text>
<target>_blank</target>
<title>城市</title>
<link></link>
<node id="p2-1-1">
<text>郊河</text>
<target>_blank</target>
<title>乡镇</title>
<link>http://www.baidu.com/</link>
</node>
</tree>
<tree id="p2-2">
<text>石家庄</text>
<target>_blank</target>
<title>城市</title>
<link></link>
</tree>
</tree>
<tree id="p3">
<text>浙江省</text>
<target>_blank</target>
<title>省份</title>
<link></link>
<tree id="p3-1">
<text>杭州市</text>
<target>_blank</target>
<title>城市</title>
<link></link>
<node id="p3-1-1">
<text>某镇</text>
<target>_blank</target>
<title>乡镇</title>
<link>http://www.baidu.com/</link>
</node>
</tree>
<tree id="p3-2">
<text>温州市</text>
<target>_blank</target>
<title>城市</title>
<link></link>
<node id="p3-2-1">
<text>某镇</text>
<target>_blank</target>
<title>乡镇</title>
<link>http://www.baidu.com/</link>
</node>
</tree>
</tree>
</treeview>
//////////////////////////////////////////////////////
然后:javascript函数实现:(文件名称:tree.htm)
<script Language="JavaScript">
var HTML = ""
var space = ""
var blank = " "
function getSubject()
{
var xmlDoc
if(window.ActiveXObject)
{
//获得操作的xml文件的对象
xmlDoc = new ActiveXObject('Microsoft.XMLDOM')
xmlDoc.async = false
xmlDoc.load("tree.xml")
if(xmlDoc == null)
{
alert('您的浏览器不支持xml文件读取,于是本页面禁止您的操作,推荐使用IE5.0以上可以解决此问题!')
window.location.href='/Index.aspx'
return
}
}
//解析xml文件,判断是否出错
if(xmlDoc.parseError.errorCode != 0)
{
alert(xmlDoc.parseError.reason)
return
}
//获得根接点
var nodes = xmlDoc.documentElement.childNodes
//得到根接点下共有子接点个数,并循环
for(var i=0i<nodes.lengthi++)
{
//如果接点名为 tree
if(nodes(i).nodeName == "tree")
{
readTree(nodes(i))
}
//如果接点名为 node
else if(nodes(i).nodeName == "node")
{
readNode(nodes(i))
}
}
//删除对象
delete(xmlDoc)
//显示HTML
window.show.innerHTML = HTML
return
}
//读Tree节点
function readTree(cI)
{
var nodes = cI.childNodes
var menuHTML = space
menuHTML += blank
//得到超级链接
menuHTML += "<a href='"
//如果该节点的连接属性不为空,则连接
if(cI.selectNodes("link")(0).text != "")
{
menuHTML += cI.selectNodes("link")(0).text
}
//否则为空链接
else
{
menuHTML += "#"
}
//目标
if(cI.selectNodes("target")(0).text != "")
{
menuHTML += " target='"+cI.selectNodes("target")(0).text
menuHTML += "'"
}
//点击菜单事件,调用divshow(vid)函数
menuHTML += " onclick=javascript:divshow('"+cI.getAttribute("id")+"')"
//得到节点标题
menuHTML += " title='"
menuHTML += cI.selectNodes("title")(0).text
//结束
menuHTML += "'>"
//得到节点的正文
menuHTML += cI.selectNodes("text")(0).text
menuHTML += "</a><br>\n"
//将menuHTML设置添加到HTML字符串
HTML += menuHTML
//得到该节点的属性值<span
HTML += "<div id='"+cI.getAttribute("id")+"' style='display:none'>\n"
for(var i=0i<nodes.lengthi++)
{
var tempImg = ""
tempImg += blank
if(nodes(i).nodeName == "tree")
{
space += tempImg
readTree(nodes(i))
space = ""
}
else if(nodes(i).nodeName == "node")
{
space += tempImg
readNode(nodes(i))
}
}
HTML += "</div>\n"
return
}
//读Node节点
function readNode(cI)
{
var nodeHTML = space
nodeHTML += blank
//设置超级链接
nodeHTML += "<a href='"
//得到连接地址
nodeHTML += cI.selectNodes("link")(0).text
//目标
if(cI.selectNodes("target")(0).text != "")
nodeHTML += "' target='"+cI.selectNodes("target")(0).text
//得到节点标题
nodeHTML += "' title='"
nodeHTML += cI.selectNodes("title")(0).text
//结束
nodeHTML += "'>"
//得到节点的正文
nodeHTML += cI.selectNodes("text")(0).text
nodeHTML += "</a><br>\n"
HTML += nodeHTML
//HTML += "<div id='"+cI.getAttribute("id")+"'>"
space = ""
return
}
//操作对象的显示还是隐藏效果
function divshow(vid)
{
if(document.all[vid].style.display == "none")
{
document.all[vid].style.display = "block"
}
else
{
document.all[vid].style.display = "none"
}
return
}
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<title>JS_XML</title>
<style type="text/css">
<!--
body
{
margin-left: 0px
margin-top: 0px
margin-right: 0px
margin-bottom: 0px
font-size: 9pt
}
A
{
text-decoration:none
font-family: "宋体"
font-size: 9pt
COLOR:#000000
}
-->
</style>
</head>
<body bgcolor="#EEEEEE" leftmargin="0" topmargin="0">
<div id=show></div>
</body>
<script>
getSubject()
</script>
</html>
//////////////////////////////////////////////////////////