Published on blog.csdn.net/zxub
At the beginning of this question, I found a lot of information and did not provide a good introduction! It took some time to finally solve this problem. I am glad that I am also deeply touched by the practicality of the materials on the Internet. I just put this blog here and hope it will be helpful to everyone.
Function openfile (filename) // open a text file and return the file content in a string
{
VaR FSO, ts, S;
VaR forreading = 1;
FSO = new activexobject ("scripting. FileSystemObject ");
TS = FSO. opentextfile (filename, forreading );
S = ts. readall ();
TS. Close ();
Return S;
}
Function SaveFile (filename, xmlstr) // Save the text file
{
VaR FSO, TF, F;
FSO = new activexobject ("scripting. FileSystemObject ");
Try
{
F = FSO. GetFile (filename );
F. Attributes = 0;
}
Catch (e ){}
TF = FSO. createtextfile (filename, true );
TF. Write (xmlstr );
TF. Close ();
}
Function savexmlfile (pfilepath, xmldom) // save as an XML file
{
VaR xmlstr = xmldom. xml;
Xmlstr = xmlstr. Replace ("<? XML version = \ "1.0 \"?> "," <? XML version = \ "1.0 \" encoding = \ "gb2312 \"?> ");
// Alert (pfilepath + "," + xmlstr );
SaveFile (pfilepath, xmlstr );
}
Function loaddom (filename)
{
VaR xmldoc = new activexobject ("msxml2.domdocument. 3.0 ");
Xmldoc. async = false;
Xmldoc. resolveexternals = false;
Xmldoc. Load (filename );
Return xmldoc;
}
// Create a DOM object based on the XML string
Function createdom (xmlstr)
{
VaR dom = new activexobject ("msxml2.domdocument. 3.0 ");
// Create a DOM instance and load the file
Dom. async = "false ";
Dom. loadxml (xmlstr );
Return Dom;
}
Function fileexist (filefullpath) // checks whether a file exists.
{
VaR FSO = new activexobject ("scripting. FileSystemObject ");
If (FSO. fileexists (filefullpath ))
{Return true ;}
Else
{Return false ;}
}
Function createxmlcfg (ptagname, pkeyname, pvalue) // Create a New XML file containing relevant information
{
VaR xmlstr = "<? XML version = \ "1.0 \" encoding = \ "gb2312 \"?> ";
Xmlstr + = "<report/> ";
VaR xmldom = createdom (xmlstr );
VaR root=xmldom.doc umentelement;
VaR infonode = xmldom. createnode (1, ptagname ,"");
Root. appendchild (infonode );
VaR tmpnode = NULL;
VaR OBJ = NULL;
Tmpnode = xmldom. createnode (1, pkeyname ,"");
Tmpnode. Text = pvalue;
Infonode. appendchild (tmpnode );
Savexmlfile (getxmlfilepath ("userdefine"), xmldom );
}
Function addxmltag (ptagname, pkeyname, pvalue) // Insert a new node containing relevant information into the XML file
{
VaR xmldom = createdom (openfile (getxmlfilepath ("userdefine ")));
VaR root=xmldom.doc umentelement;
VaR infonode = xmldom. createnode (1, ptagname ,"");
Root. appendchild (infonode );
VaR tmpnode = NULL;
VaR OBJ = NULL;
Tmpnode = xmldom. createnode (1, pkeyname ,"");
Tmpnode. Text = pvalue;
Infonode. appendchild (tmpnode );
VaR xmlstr = xmldom. xml;
Xmlstr = xmlstr. Replace ("<? XML version = \ "1.0 \"?> "," <? XML version = \ "1.0 \" encoding = \ "gb2312 \"?> ")
SaveFile (getxmlfilepath ("userdefine"), xmlstr );
}
Function addxmlkey (ptagname, pkeyname, pvalue) // Insert new information to related nodes in XML
{
VaR xmldom = createdom (openfile (getxmlfilepath ("userdefine ")));
VaR pnodelist = xmldom. getelementsbytagname (ptagname );
VaR pchildnode = pnodelist. Item (0 );
VaR tmpnode = NULL;
VaR OBJ = NULL;
Tmpnode = xmldom. createnode (1, pkeyname ,"");
Tmpnode. Text = pvalue;
Pchildnode. appendchild (tmpnode );
VaR xmlstr = xmldom. xml;
Xmlstr = xmlstr. Replace ("<? XML version = \ "1.0 \"?> "," <? XML version = \ "1.0 \" encoding = \ "gb2312 \"?> ")
SaveFile (getxmlfilepath ("userdefine"), xmlstr );
}
Function getxmlfilepath (filetype) // obtain the path of the configuration file: filetype indicates the file type, default is default. XML, and userdefine is the same
{
VaR dir = location. href. substring (0, location. href. lastindexof ('? ') + 1 );
VaR xmlfilepath = location. href. substring (dir. length, location. href. Length + 1 );
Xmlfilepath = xmlfilepath. Replace ("file :///","");
Xmlfilepath = xmlfilepath. substring (0, xmlfilepath. lastindexof ("/"));
Xmlfilepath = xmlfilepath. substring (0, xmlfilepath. lastindexof ("/"));
Switch (filetype ){
Case "default ":
Xmlfilepath = xmlfilepath + "/config/Default. xml ";
Break;
Case "userdefine ":
Xmlfilepath = xmlfilepath + "/config/userdefine. xml ";
Break
Default:
Alert ("the default file or user-defined file is not clear! ");
Return false;
}
Xmlfilepath = xmlfilepath. Replace ("/","//");
Return (xmlfilepath );
}
Function setvaluebyname (filetype, ptagname, pkeyname, pvalue) // location assignment in XML file
{
VaR xmldom;
If (fileexist (getxmlfilepath (filetype )))
{
Xmldom = createdom (openfile (getxmlfilepath (filetype )));
}
Else
{
// Alert ("the user configuration file does not exist. A new file will be created ");
// Create a file
Createxmlcfg (ptagname, pkeyname, pvalue );
Alert ("the modified information has been saved. In the future, A4 paper will be printed based on the modified configuration! ");
Return;
}
Try
{
VaR pnodelist = xmldom. getelementsbytagname (ptagname );
If (pnodelist. Item (0) = NULL)
{
// Alert ("This table information does not exist! ");
Addxmltag (ptagname, pkeyname, pvalue );
Alert ("the modified information has been saved. In the future, A4 paper will be printed based on the modified configuration! ");
Return;
}
VaR pchildnode = pnodelist. Item (0). firstchild;
For (j = 1; j ++)
{
If (pchildnode. nodename = pkeyname)
{
Pchildnode. Text = pvalue;
Break;
}
Pchildnode = pchildnode. nextsibling;
}
VaR xmlstr = xmldom. xml;
Xmlstr = xmlstr. Replace ("<? XML version = \ "1.0 \"?> "," <? XML version = \ "1.0 \" encoding = \ "gb2312 \"?> ")
SaveFile (getxmlfilepath (filetype), xmlstr );
Alert ("the modified information has been saved. In the future, A4 paper will be printed based on the modified configuration! ");
}
Catch (E)
{
// Alert ("This table prints that the configuration node does not exist! ");
Addxmlkey (ptagname, pkeyname, pvalue );
Alert ("the modified information has been saved. In the future, A4 paper will be printed based on the modified configuration! ");
Return;
}
}
Function getvaluebyname (filetype, ptagname, pkeyname) // obtain the value of a specific node.
{
VaR curfiletype = filetype; // the current configuration file type.
VaR xmldom;
If (fileexist (getxmlfilepath (filetype )))
{
Xmldom = createdom (openfile (getxmlfilepath (filetype )));
}
Else
{
If (fileexist (getxmlfilepath ("default ")))
{
// Alert ("the User-Defined setting file does not exist. The default setting will be used! ");
Xmldom = createdom (openfile (getxmlfilepath ("default ")));
Curfiletype = "default ";
}
Else
{
Alert ("the print settings file does not exist. Please update the system online to obtain the default settings file, or save the custom settings in page adjustment to create a custom print configuration file! ");
Return false;
}
}
Try
{
VaR pnodelist = xmldom. getelementsbytagname (ptagname );
// Alert (pnodelist. Item (0). nodename );
VaR pchildnode = pnodelist. Item (0). firstchild;
VaR pvalue;
For (j = 1; j ++)
{
If (pchildnode. nodename = pkeyname)
{
Pvalue = pchildnode. text;
Return pvalue;
Break;
}
Pchildnode = pchildnode. nextsibling;
}
}
Catch (E)
{
If (curfiletype = "default") // The default configuration file does not contain configuration for this table.
{
Alert ("the settings for this table do not exist in the print settings file. Please update the system online to obtain the latest default settings file, you can also save the custom settings in the page adjustment to create a custom print configuration file! ");
Return false;
}
Else
{
// Alert ("the custom print settings do not exist for this table. The default value is used! ");
Return getvaluebyname ("default", ptagname, pkeyname );
}
}
}