Can I add, modify, or delete a node to an XML file based on the client's JavaScript?
This can be done. I will give it in the following example. In this example, we try to use all the operations and attributes for Demonstration:
<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 );
}
}
// 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>