<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title></title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1"> <meta name="vs_targetSchema" content="</head> <body> <script language=javascript> <!--
var doc = new ActiveXObject("MSXML2.DOMDocument"); function writeXML() { // var doc = new ActiveXObject("Msxml2.DOMDocument"); // doc.load("tmp.xml"); var doc = new ActiveXObject("MSXML2.DOMDocument"); var p = doc.createProcessingInstruction("xml","version=‘1.0‘ encoding=‘gb2312‘"); doc.appendChild(p); var root = doc.createNode(1,"中國",""); doc.appendChild(root); var elem=doc.createNode(1,"省",""); root.appendChild(elem); var att=doc.createAttribute("名稱"); att.value="福建"; elem.setAttributeNode(att); var ele1=doc.createNode(1,"城市",""); ele1.text="廈門"; elem.appendChild(ele1); var ele2=doc.createNode(1,"城市",""); ele2.text="石獅"; elem.appendChild(ele2); var ele3=doc.createNode(1,"城市",""); ele3.text="晉江"; elem.appendChild(ele3);
var elem=doc.createNode(1,"省",""); root.appendChild(elem); var att=doc.createAttribute("名稱"); att.value="江蘇"; elem.setAttributeNode(att);
var elem=doc.createNode(1,"省",""); root.appendChild(elem); var att=doc.createAttribute("名稱"); att.value="安徽"; elem.setAttributeNode(att);
doc.save("china.xml"); }
function readXML(){ var doc = new ActiveXObject("MSXML2.DOMDocument"); doc.load ("china.xml") var list = doc.selectNodes("/中國/省[@名稱=‘福建‘]/城市") for(var i=0;i<list.length;i++) { alert(list[i].text); } }
//--> </script> <br><br><br><br><br><br> <center> <input type=button value="寫入XML" onclick="writeXML()" id=button1 name=button1> <input type=button value="讀取XML" onclick="readXML()" id=button2 name=button2> <center> </body> </html>
|