Import org. JDOM .*;
Import org. JDOM. Input. saxbuilder;
Import org. JDOM. Output. xmloutputter;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
/**
* Retrieve the configuration password XML
* @ Param attrname
* @ Return
*/
Protected string getstandardpwd (string attrname ){
If (E = NULL ){
Try {
Servletcontext S = (servletcontext) facescontext. getcurrentinstance (). getexternalcontext (). getcontext (); // obtain the server path
File F = new file (s. getrealpath ("/") + "Resources" + file. separatorchar + "useconfig. xml"); // obtain the XML file path
Saxbuilder sb = new saxbuilder ();
Document Doc = sb. Build (f );//
E = Doc. getrootelement (). getchild ("userconfig"); // obtain the pureftpd (getchild) subnode under the getrootelement node.
} Catch (exception ex ){
Return NULL;
}
}
Return e. getchild (attrname). getattribute ("value"). getvalue (); // obtain the attribute value of the node.
}
/**
* Write an XML file
* @ Param attrname
*/
Protected void setstandardpwd (string attrname, string randnumber ){
Try {
Servletcontext S = (servletcontext) facescontext. getcurrentinstance (). getexternalcontext (). getcontext ();
String Path = S. getrealpath ("/") + "Resources" + file. separatorchar + "useconfig. xml"; // obtain the XML file path
Fileinputstream Fi = new fileinputstream (PATH); // write the file to the stream
Saxbuilder sb = new saxbuilder ();
Document Doc = sb. Build (FI );//
E = Doc. getrootelement (). getchild ("userconfig"); // obtain the pureftpd (getchild) subnode under the getrootelement node.
E. getchild (attrname). getattribute ("value"). setvalue (randnumber); // set the attribute to a random number.
Fileoutputstream FZ = NULL;
String indent = "";
Boolean newlines = true;
Xmloutputter outp = new xmloutputter (indent, newlines, "GBK ");
FZ = new fileoutputstream (PATH); // output stream
Outp. Output (Doc, Fz); // Save the XML File Modification
Fi. Close ();
FZ. Close ();
} Catch (exception ex ){
System. Out. println (ex. tostring ());
}
}