Operate xml with javascript

Source: Internet
Author: User

Try to use all the operations and attributes for demonstration.

Copy codeThe Code is as follows: <script language = "JavaScript">
<! --
Var doc = new ActiveXObject ("Msxml2.DOMDocument"); // ie5.5 +, CreateObject ("Microsoft. XMLDOM ")

// Load the document
// Doc. load ("B. xml ");

// Create a file header
Var p = doc. createProcessingInstruction ("xml", "version = '1. 0' encoding = 'gb2312 '");

// Add a File Header
Doc. appendChild (p );

// Obtain the root contact during direct loading.
// Var root = doc.doc umentElement;

// Create a root contact in two ways
// Var root = doc. createElement ("students ");
Var root = doc. createNode (1, "students ","");

// Create a child contact
Var n = doc. createNode (1, "ttyp ","");

// Specify the stator contact text
// N. text = "this is a test ";

// Create a sun contact
Var o = doc. createElement ("sex ");
O. text = "male"; // specify the text

// Create attributes
Var r = doc. createAttribute ("id ");
R. value = "test ";

// Add attributes
N. setAttributeNode (r );

// Create the second property
Var r1 = doc. createAttribute ("class ");
R1.value = "tt ";

// Add attributes
N. setAttributeNode (r1 );

// Delete the second property
N. removeAttribute ("class ");

// Add a sun contact
N. appendChild (o );

// Add text contacts
N. appendChild (doc. createTextNode ("this is a text node ."));

// Add comments
N. appendChild (doc. createComment ("this is a comment \ n "));

// Add a Child Contact
Root. appendChild (n );

// Copy contacts
Var m = n. cloneNode (true );

Root. appendChild (m );

// Delete the contact
Root. removeChild (root. childNodes (0 ));

// Create a data segment
Var c = doc. createCDATASection ("this is a cdata ");
C. text = "hi, cdata ";
// Add Data Segment
Root. appendChild (c );

// Add the root contact
Doc. appendChild (root );

// Search for contacts
Var a = doc. getElementsByTagName ("ttyp ");
// Var a = doc. selectNodes ("// ttyp ");

// Display the attributes of the change 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 );
}
}

// Modify the node and use XPATH to locate the node
Var B = doc. selectSingleNode ("// ttyp/sex ");
B. text = "female ";

// Alert (doc. xml );

// Save the XML file (the file must be stored on the server and FSO must be used on the client)
// Doc. save ();

// View the root contact XML
If (n)
{
Alert (n. ownerDocument. xml );
}

// -->
</Script>

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.