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>