Java read/write XML

Source: Internet
Author: User

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 ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.