Example shows how to generate XML with JavaScript

Source: Internet
Author: User
Tags cdata header
javascript|xml| generates the XML text as much as possible using all the actions and attributes to make a summary:

<script language= "JavaScript" > <!--var doc = new ActiveXObject ("Msxml2.domdocument"); Ie5.5+,createobject ("Microsoft.XMLDOM")//Loading document//doc.load ("B.xml");     Create file header var p = doc.createprocessinginstruction ("xml", "version= ' 1.0 '" encoding= ' gb2312 ' "); Add File Header Doc.appendchild (p); For direct loading, the root contact//var root = doc.documentelement is obtained;     Two ways to create a root contact//var root = doc.createelement ("Students");     var root = Doc.createnode (1, "Students", "");         Create Child contacts var n = doc.createnode (1, "Ttyp", "");          Specifies the sub-contact Text//n.text = "This is a test";         Create Sun contact var o = doc.createelement ("Sex");      O.text = "male";         Specify its text//create attribute var r = doc.createattribute ("id");         r.value= "Test";     Add attribute N.setattributenode (r);         Create a second property var r1 = Doc.createattribute ("class");                  R1.value= "TT";         Add attribute N.setattributenode (R1);         Deletes the second property N.removeattribute ("class"); Add Sun Contact N.appendChild (O);         Adds a text contact N.appendchild ("This is a text node.") (Doc.createtextnode);              Add Comment n.appendchild (doc.createcomment ("This is a comment\n"));          Add a sub contact root.appendchild (n);         Copy contact var m = N.clonenode (true);                  Root.appendchild (m);     Delete Contact Root.removechild (root.childnodes (0));         Create data segment var c = doc.createcdatasection ("This is a CDATA");         C.text = "Hi,cdata";          Add a data Segment Root.appendchild (c);     Add Root contact Doc.appendchild (root);     Lookup contact var a = Doc.getelementsbytagname ("Ttyp");     var a = Doc.selectnodes ("//ttyp");          Displays the properties for the change point for (Var i= 0;i<a.length;i++) {alert (a[i].xml); for (Var j=0;j<a[i].attributes.length;j++)         {alert (a[i].attributes[j].name);          }//xml Save (required on the server, client with FSO)//doc.save ();     View the root contact XML if (n) {alert (n.ownerdocument.xml); }//--> </script&Gt 


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.