In this article, we try to use all the operations and attributes for summary. <Script language = "JavaScript"> <! -- VaR Doc = new activexobject ("msxml2.domdocument"); // ie5.5 +, Createobject ("Microsoft. xmldom ") // Load the document Doc. Load ("C:/tomcat5/webapps/CBF/B. xml "); Doc. async = false; Doc. resolveexternals = false; Doc. validateonparse = false; // 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 ); } } // Save the XML file (the file must be stored on the server and FSO must be used on the client) Doc. Save ("C:/tomcat5/webapps/CBF/B. xml ");
// View the root contact XML If (N) { Alert (N. ownerdocument. XML ); } // --> </SCRIPT> |