提示:您可以先修改部分代碼再運行
<p><!-----------------小馬哥xpath對ajax的完全操作,添加修改刪除xml節點---------------------><br /><button onclick="alert(xml.lookxml())" id=button1 name=button1>查看XML標籤</button><br /><hr><br />添加節點:姓名:<input type=text id="name"> 內容:<input type=text id="content"> <input type=button value="添加XML節點" onclick="addnode()" id=button3 name=button3><br /><hr><br />xpath查詢:<input type=text id="sql"><input value="xpath查詢" type=button onclick="Ctable(oo('sql').value)" id=button4 name=button4><br /><hr><br /><div id=createTable></div><br /><script><br />var isIE=navigator.userAgent.indexOf("IE")>-1?true:false<br />function CAJAX(){<br /> this.createDomDoc=function(){<br /> var signatures=["Msxml2.DOMDocument.5.0","Msxml2.DOMDocument.4.0","Msxml2.DOMDocument.3.0","Msxml2.DOMDocument","Microsoft.XmlDom"];<br /> for(var i=0;i<signatures.length;i++){try{var domDoc=new ActiveXObject(signatures[i]);return domDoc;}catch(e){}}<br /> return null;<br /> }<br /> this.SelectNodes = function(xpath){<br /> if (isIE)<br /> return domDoc.selectNodes(xpath) ;<br /> else{<br /> var aNodeArray = new Array();<br /> var xPathResult = domDoc.evaluate(xpath, domDoc, domDoc.createNSResolver(domDoc.documentElement), XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;<br /> if (xPathResult){<br /> var oNode = xPathResult.iterateNext() ;<br /> while(oNode){<br /> aNodeArray[aNodeArray.length] = oNode ;<br /> oNode = xPathResult.iterateNext();<br /> }<br /> }<br /> return aNodeArray ;<br /> }<br /> }<br /> this.addxml=function(doc,tagName,tagText){<br /> var node=domDoc.createElement(tagName);<br /> if(tagText!=null){<br /> if(isIE)<br /> node.text=tagText;<br /> else<br /> node.textContent=tagText;<br /> }<br /> doc=doc==null?domDoc:doc<br /> doc.appendChild(node);<br /> }<br /> this.lookxml=function(){<br /> if(isIE){return domDoc.xml;}<br /> else{<br /> var serializer=new XMLSerializer();<br /> var xml=serializer.serializeToString(domDoc)<br /> return xml<br /> }<br /> }<br /> this.xva=function(xpath){return this.SelectNodes(xpath)} //擷取所有節點<br /> this.o=function(xpath,n){n=n==null?0:n;return this.SelectNodes(xpath)[n]} //擷取單個節點<br /> this.xv=function(xpath,n){try{return this.getText(this.SelectNodes(xpath)[n==null?0:n])}catch(e){return ""}} //擷取單個節點內容<br /> this.getText=function(obj){return obj.firstChild.nodeValue} //擷取節點的內容<br /> this.getkey=function(obj,key){var o=obj.getAttribute(key);return o==null?"":o} //擷取節點屬性<br /> this.setText=function(obj,text){obj.firstChild.nodeValue=text} //設定節點內容<br /> this.setkey=function(obj,key,text){obj.setAttribute(key,text)}<br /> <br /> var domDoc=isIE?this.createDomDoc():document.implementation.createDocument("","",null) //建立對象<br /> this.addxml(null,"root",null);<br />}<br />var xml=new CAJAX()<br />/*xml.addxml(xml.xva("/root")[0],"title","majiok2")<br />xml.addxml(xml.xva("/root")[0],"title","majiok2")<br />xml.setText(xml.o("//title"),"toLocaleLowerCaseX")<br />xml.setkey(xml.o("//title"),"id","maji")<br />*/<br />function oo(obj){return document.getElementById(obj)}<br />function addnode(){<br /> var n=xml.xva("//root/item").length<br /> xml.addxml(xml.o("/root"),"item",oo("content").value==""?"null":oo("content").value)<br /> xml.setkey(xml.o("//item",n),"name",oo("name").value)<br /> Ctable("//item")<br />}<br />function updatenode(n){<br /> var obj=xml.o("//item",n)<br /> xml.setText(obj,oo("content_"+n).value)<br /> xml.setkey(obj,"name",oo("name_"+n).value)<br /> alert("修改成功!")<br />}<br />function delnode(n,xpath){<br /> var obj=xml.o(xpath,n)<br /> obj.parentNode.removeChild(obj)<br /> Ctable(xpath)<br />}<br />function Ctable(Xpath){<br /> var str="<table width='90%' border='0' cellspacing='1' cellpadding='3' bgcolor='#333333'>\<br /> <tr bgcolor='#E8F3FF'> \<br /> <td>序號</td>\<br /> <td>姓名(name)</td>\<br /> <td>內容(content)</td>\<br /> <td>操作</td>\<br /> </tr>"<br />// try{<br /> var a=xml.xva(Xpath)<br /> if(a.length==0)<br /> str+="<tr bgcolor='#FFFFFF'><td colspan=4>沒有相關資訊</td></tr></table>"<br /> else{<br /> for(var i=0;i<a.length;i++){<br /> str+=" <tr bgcolor='#FFFFFF'> \<br /> <td>"+(i+1)+"</td>\<br /> <td><input id='name_"+i+"' value='"+xml.getkey(a[i],"name")+"'></td>\<br /> <td><input id='content_"+i+"' value='"+xml.getText(a[i])+"'></td>\<br /> <td><a href='javascript:updatenode("+i+")'>修改</a> <a href=javascript:delnode("+i+",'"+Xpath+"')>刪除</td>\<br /> </tr>"<br /> }<br /> str+="</table>"<br /> }<br /> oo("createTable").innerHTML=str<br />// }catch(e){alert("xpath語句錯誤")}<br />}<br />Ctable("//item")<br /></script></p>
提示:您可以先修改部分代碼再運行