Dom4j XML operations

Source: Internet
Author: User
Boolean flag = budgetcommon. createfolderfile ("C: \ mfmis \ defaulsetting \ defaulbudgetorginconfig. XML "); //" + format. format (new date () + "If (FLAG) {// if true is returned, the file in this directory does not exist. For the first time, create budgetcommon. savedefaulxml (defaulsettingform, defaulsettingdialog);} else {// If the returned value is false, it indicates that the files in this directory exist in budgetcommon. savedefaulxml (defaulsettingform, defaulsettingdialog );}
/*** Create a specified file from a specified path */Public Boolean createfolderfile (string filename) {file = new file (filename); If (file. exists () {// system. out. println ("creating file" + filename + "failed. The target file already exists! "); Return false;} If (filename. endswith (file. separator) {// system. out. println ("creating file" + filename + "failed. The target cannot be a directory! "); Return false;} // If the directory does not exist, create this directory if (! File. getparentfile (). exists () {// system. Out. println ("The path of the target file does not exist. You are about to create a file... "); If (! File. getparentfile (). mkdirs () {// system. Out. println ("An error occurred while creating the directory where the directory file is located! "); Return false ;}}// create the target file try {If (file. createnewfile () {// system. out. println ("file created" + filename + "succeeded! "); Return true;} else {// system. Out. println (" File Creation "+ filename +" failed! "); Return false ;}} catch (ioexception e) {e. printstacktrace (); // system. Out. println (" File Creation "+ filename +" failed! "); Return false ;}}
/*** Save the data to XML * @ Param defaulsettingform * @ Param defaulsettingdialog * @ return */Public Boolean savedefaulxml (Form defaulsettingform, budgetorgindefaulsetting defaulsettingdialog) {try {outputformat format = outputformat. createprettyprint (); // format. setencoding ("UTF-8"); // xmlwriter output = new xmlwriter (New filewriter (new file ("C: \ mfmis \ defaulsetting \ defaulbudgetorginconfig. XML "), format); XML Writer output = new xmlwriter (New outputstreamwriter (New fileoutputstream ("C: \ mfmis \ defaulsetting \ defaulbudgetorginconfig. XML ")," UTF-8 "), format); output. write (createdefauldocument (defaulsettingform); output. close (); joptionpane. showmessagedialog (null, "The default value is set successfully! "," Message prompt ", joptionpane. information_message); defaulsettingdialog. removemousewheellistener (this); defaulsettingdiener. addmousewheellistener (this); defaulsettingdiener. dispose (); Return true;} catch (exception E1) {joptionpane. showmessagedialog (null, "An error occurred while setting the default value! "," Message prompt ", joptionpane. information_message); e1.printstacktrace (); Return false ;}}
/*** Traverse all controls in form * take out its attributename and value * and put it into the XML file */public document createdefauldocument (container C) {// use dom4j to create the initnet element document = incluenthelper. createdocument (); element root = document. addelement ("root"); // use dom4j to create the root node try {for (Component component: C. getcomponents () {If (Component instanceof textcontrol) {// text box textcontrol = (textcontrol) component); If (textco Ntrol. gettext ()! = NULL &&! "". Equals (textcontrol. gettext () {// Add the child node element = root. addelement (textcontrol. getattributename (); element. settext (textcontrol. gettext () ;}} else if (Component instanceof currencycontrol) {currencycontrol = (currencycontrol) component);} else if (Component instanceof codlookupcontrol) {// drop-down box codlookupcontrol codlookup = (codlookupcontrol) component);/* If ("hidden ". equals (FLAG )) {Continue;} */If (codlookup. getvalue ()! = NULL) {// Add the child node element = root. addelement (codlookup. getattributename (); element. settext (codlookup. getvalue (). tostring () ;}} else if (Component instanceof comboboxcontrol) {// drop-down box comboboxcontrol ComboBox = (comboboxcontrol) component);} else if (Component instanceof datecontrol) {// Date box datecontrol = (datecontrol) component); simpledateformat sdfin = new simpledateformat ("yyyy/mm /DD ");} else if (Component instanceof treepanelcontrol) {// drop-down box treepanelcontrol = (treepanelcontrol) component; Map map = treepanelcontrol. getcolumnfilters (); string elementcode = treepanelcontrol. getelementcode (); string columncode = treepanelcontrol. getcolumncode (); If (map. get (elementcode )! = NULL) {element = root. addelement (columncode); element. settext (map. get (elementcode ). tostring () + "@" + treepanelcontrol. getcolumntext (). gettext () ;}}} catch (exception e) {e. printstacktrace ();} return document ;}/**
public void claerDefaulXML(){try {File file = new File("C:\\mfmis\\DefaulSetting\\DefaulBudgetorginConfig.xml");file.delete();/*OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter output = new XMLWriter(new FileWriter(new File("C:\\mfmis\\DefaulSetting\\DefaulBudgetorgin.xml")),format);output.write("");output.close();*/} catch (Exception e) {e.printStackTrace();}}


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.