XPath complete operation of Ajax, add modify Delete XML node

Source: Internet
Author: User
Tags gettext tagname xpath
Tip: You can modify some of the code before running

<p><!-----------------Pony XPath full manipulation of Ajax, adding modifications to delete XML nodes---------------------><br/> <button onclick= "alert (Xml.lookxml ())" Id=button1 name=button1> view XML tags </button><br/> <pr><br/> Adding nodes: Name: <input type=text id= "name" > Content: <input type=text id= "content > <input type=button value=" Add XML node "Onclick=" AddNode () id=button3 name=button3><br/> <pr><br/> XPath query: <input type=text id= " SQL "><input value=" XPath query "Type=button onclick=" CTable (oo (' sql '). Value) "Id=button4 name=button4><br/ > <pr><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/>,  & nbsp; for (Var i=0;i<signatures.length;i++) {Try{var domdoc=new ActiveXObject (signatures[i)); catch (e) {}}<br/>   return null;<br/>  }<br/>. selectnodes = function (XPath) {<br/>   if (isie) <br/> Domdoc.selectnodes (XPath); <br/>   else{<br/>    var = 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; &LT;BR/ >      onode = Xpathresult.iteratenext (); <br/> &NBSP;&NBSP;&NBSP;&NBSP;}&LT;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/> 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)}               / /Get all Nodes <br/>  this.o=function (xpath,n) {N=n==null?0:n;return this. SelectNodes (XPath) [n]}           //gets a single node <br/>  this.xv=function (xpath,n) {Try{return This.gettext (this. SelectNodes (XPath) [n==null?0:n])}catch (e) {return ""}}  //get a single node content <br/>  this.gettext= function (obj) {return obj.firstchild.nodevalue}                //gets the contents of the node <br/>  this.getkey=function (obj,key) {var o=obj.getAttribute ( key); return o==null? ": o}           //get node Properties <br/>  this.settext=function (obj , text) {obj.firstchild.nodevalue=text}               //set node content <br/>  this.setkey=function (obj,key,text) {Obj.setattribute (key,text)}<br/>  <br/>  var domdoc=isie?this.createdomdoc (): Document.implementation.createDocument ("", "", null)         //Create object <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") [0], "title", "/root") Majiok2 <br Xml.settext (XML.O ("//title"), "Tolocalelowercasex" <br/> ("Xml.setkey"), "id", "XML.O")//title/ > */<br/> Function oo (obj) {return document.getElementById (obj)}<br/> Function AddNode () {<br/> & Nbsp;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/> (obj, "name", OO ("name_" +n). Value) <br/>  alert ("modified successfully!") <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> serial number </td>\ <br/>      <td> name (name) </td>\<br/>      <td> Contents (content) </td>\<br/>      <td> operations </td>\<br/>     </tr> "<br/>// try{<br/>   var a=xml.xva (Xpath) <br/>-  if ( a.length==0) <br/> &Nbsp;  str+= "<tr bgcolor= ' #FFFFFF ' ><td colspan=4> no relevant information </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+") ' > Modify </a> <a href=javascript:delnode ("+i+", ' "+xpath+") > Delete </td>\ <br/>         </tr> "<br/>    }<br/>     str+= "</table>" <br/>   }<br/>   oo (" CreateTable "). Innerhtml=str<br/>// }catch (e) {alert (" XPath statement error ")}<br/> A}<br/> ctable ("// Item ") <br/> </script></p>
Tip: You can modify some of the code before running
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.