Fso|xml
Try it, Xmldoc.save () line is different, try to use FSO to do out.
Tidy up, for everyone to discuss.
Because the local XML file with JS operation after save will permission denied, so as far as I know only through the FSO to do. With the FSO, the file is basically independent of whether it is XML.
XML file:
<root>
<parent>
<child1>one</child1>
<child2>one</child2>
</parent>
<parent>
<child1>two</child1>
<child2>two</child2>
</parent>
<parent>
...
</parent>
</root>
var temp;//the data to manipulate
//Add node
function Add (root,parent,child1,child2) {
try{
var fso = new ActiveXObject (" Scripting.FileSystemObject ");
var FileOpen = fso. OpenTextFile ("*.xml", 1);
var filecontent = Fileopen.readall ();
fileopen.close ();
if (filecontent.tostring (). IndexOf (temp)!=-1) {
Alert (temp+ "is already in data.");
}else{
Filecontent = Filecontent.replace ("</" +root+ ">", "");
Filecontent + + "<" +parent+ "><" +child1+ ">";
Filecontent = = ...;
Filecontent + + "</" +child1+ "><" +child2+ ">";
Filecontent = = ...;
Filecontent + + "</" +child2+ "></" +parent+ "></" +root+ ">";
var filewrite = fso. OpenTextFile ("*.xml", 2)
Filewrite.write (filecontent);
Filewrite.close ();
Alert ("Add successfully!")
}
}catch (e) {
Alert ("Fail to Add.");
}
}
Delete a node
Function del (root,parent,child1,child2) {
try{
if (Confirm ("Are you sure to delete symbol: +temp+")) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var fileOpen = fso. OpenTextFile ("*.xml", 1);
var filecontent = Fileopen.readall ();
Fileopen.close ();
var Parentarr = filecontent.split ("</" +parent+ ">");
var Symbolpos = -2;//is not necessarily 2, as long as <0 can
for (var i=0; i<parentarr.length-1; i++) {
if (Parentarr[i].indexof (temp)!=-1) {
Symbolpos = i;
Continue
}
Parentarr[i] + + "</" +parent+ ">";
}
if (Symbolpos = = 2) {
Alert (temp+ "not found in data.")
}else{
if (Symbolpos = = 0) {
Parentarr[0] = "<" +root+ ">";
}else{
Parentarr.splice (symbolpos,1);
}
Filecontent = parentarr.tostring (). Replace (/,/g, "")
var filewrite = fso. OpenTextFile ("*.xml", 2);
Filewrite.write (filecontent);
Filewrite.close ();
Alert ("Deleted successfully!");
}
}
}catch (e) {
Alert ("Fail to delete.");
}
Basic ideas:
Add node
1: Read all characters of original XML file, save as Filecontent;
2: To remove the root node of the closed nodes, that is </root>;
3:filecontent + = <parent><child1> </child1><child2>...</child2></parent>;
4:filecontent + </root>;
5: Write XML file, close.
It's basically a manual input to replace the XML CreateNode.
Delete a node
1: read all characters of original XML file, save as Filecontent;
2: to Parent to split Filecontent, save for parentarr[];
3: cyclic search parentarr[i], using indexof (Parentarr[i]) to determine whether the content of the node to be deleted appears in parentarr[i], if not found, then Parentarr[i = Parent is used to make up the lost parent in the split, and if found, do not use + = parent, and record the value of I;
4:parentarr.splice (i,1), delete node;
5:parentarr.tostring (). Replace (/,/g, ""), used to remove commas;
6: write XML file, close.
Note: pay attention to the problem that the node of the split is not easy to match, so parentarr[i] = parent should be careful. Of course, split is not necessarily according to parent, which node is clear according to which split.
It looks as if the JS operation of the local file does not make sense, oh, but because see as if not how many people use FSO to operate the XML, so try, change ideas, peacetime fso used very little. Welcome to criticize:D