Using JavaScript to manipulate xml_javascript techniques

Source: Internet
Author: User
Tags cdata

Try to use all the operations and attributes to do the demonstration

Copy Code code as follows:
<script language= "JavaScript" >
<!--
var doc = new ActiveXObject ("Msxml2.domdocument"); Ie5.5+,createobject ("Microsoft.XMLDOM")


Loading documents
Doc.load ("B.xml");

To create a file header
var p = doc.createprocessinginstruction ("xml", "version= ' 1.0 ' encoding= ' gb2312 '");

Add File Headers
Doc.appendchild (P);

To get the root contact when loading directly
var root = doc.documentelement;

Create root contacts in two ways
var root = doc.createelement ("Students");
var root = Doc.createnode (1, "Students", "");

Creating Child contacts
var n = doc.createnode (1, "Ttyp", "");

Specify Child Contact text
N.text = "This is a test";

Create Sun Contact
var o = doc.createelement ("Sex");
O.text = "male"; Specify its text

Creating properties
var r = doc.createattribute ("id");
r.value= "Test";

Add properties
N.setattributenode (R);

Create a second property
var r1 = Doc.createattribute ("class");
R1.value= "TT";

Add properties
N.setattributenode (R1);

Delete Second Property
N.removeattribute ("class");

Add Sun Contact
N.appendchild (o);

Add text Contacts
N.appendchild (Doc.createtextnode ("This is a text node.");

Add a Comment
N.appendchild (Doc.createcomment ("This is a comment\n"));

Add Child contacts
Root.appendchild (n);

Copy Contacts
var m = N.clonenode (true);

Root.appendchild (m);

Delete a contact
Root.removechild (root.childnodes (0));

Create a data segment
var c = doc.createcdatasection ("This is a CDATA");
C.text = "Hi,cdata";
Adding data segments
Root.appendchild (c);

Add Root Contact
Doc.appendchild (root);

Find Contacts
var a = Doc.getelementsbytagname ("Ttyp");
var a = Doc.selectnodes ("//ttyp");

Show the properties of the modified contact
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);
}
}

modifying nodes, using XPath to locate nodes
var B = Doc.selectsinglenode ("//ttyp/sex");
B.text = "female";

alert (doc.xml);

XML save (required on server, client with FSO)
Doc.save ();

View the root contact XML
if (n)
{
alert (n.ownerdocument.xml);
}

-->
</script>

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.