Dom4j操作xml

來源:互聯網
上載者:User

標籤:java   使用   os   檔案   io   資料   for   ar   

boolean flag = budgetCommon.createFolderFile("C:\\mfmis\\DefaulSetting\\DefaulBudgetorginConfig.xml");//"+format.format(new Date())+"if(flag){//若返回為true,則代表此目錄下的檔案不存在,為第一次建立budgetCommon.saveDefaulXML(defaulSettingForm,defaulSettingDialog);}else{//若返回 為false,則代表此目錄下的檔案存在budgetCommon.saveDefaulXML(defaulSettingForm,defaulSettingDialog);}
/** * 向指定路徑建立指定檔案 */public boolean createFolderFile(String fileName) {File file = new File(fileName);if (file.exists()) {//System.out.println("建立檔案" + fileName + "失敗,目標檔案已存在!");return false;}if (fileName.endsWith(File.separator)) {//System.out.println("建立檔案" + fileName + "失敗,目標不能是目錄!");return false;}//如果目錄不存在,則建立這個目錄if (!file.getParentFile().exists()) {//System.out.println("目標檔案所在路徑不存在,準備建立。。。");if (!file.getParentFile().mkdirs()) {//System.out.println("建立目錄檔案所在的目錄失敗!");return false;}}// 建立目標檔案try {if (file.createNewFile()) {///System.out.println("建立檔案" + fileName + "成功!");return true;} else {///System.out.println("建立檔案" + fileName + "失敗!");return false;}} catch (IOException e) {e.printStackTrace();//System.out.println("建立檔案" + fileName + "失敗!");return false;}}
/** * 將資料儲存至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);XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream("C:\\mfmis\\DefaulSetting\\DefaulBudgetorginConfig.xml"),"utf-8"),format);output.write(createDefaulDocument(defaulSettingForm));output.close();JOptionPane.showMessageDialog(null, "預設值設定成功!","資訊提示",JOptionPane.INFORMATION_MESSAGE);defaulSettingDialog.removeMouseWheelListener(this);defaulSettingDialog.addMouseWheelListener(this);defaulSettingDialog.dispose();return true;} catch (Exception e1) {JOptionPane.showMessageDialog(null, "預設值設定失敗!","資訊提示",JOptionPane.INFORMATION_MESSAGE);e1.printStackTrace();return false;}}
/** * 遍曆Form中的所有控制項 * 拿出其attributeName和value * 將其放入xml檔案中 */public Document createDefaulDocument(Container c){//使用DOM4J建立Documnet元素 Document document = DocumentHelper.createDocument(); Element root = document.addElement("root");//使用DOM4J建立root結點  try {for(Component component : c.getComponents()){if (component instanceof TextControl) {// 文字框TextControl textControl = ((TextControl) component);if(textControl.getText()!=null&&!"".equals(textControl.getText())){//添加子結點Element element = root.addElement(textControl.getAttributeName());element.setText(textControl.getText());}} else if(component instanceof CurrencyControl){CurrencyControl currencyControl=((CurrencyControl)component);}else if (component instanceof CodLookupControl) {// 下拉彈出框CodLookupControl codLookup = ((CodLookupControl) component);/*if("HIDDEN".equals(flag)){continue ; }*/if(codLookup.getValue()!=null){//添加子結點Element element = root.addElement(codLookup.getAttributeName());element.setText(codLookup.getValue().toString());}} else if (component instanceof ComboBoxControl) {// 下拉選擇框ComboBoxControl comboBox = ((ComboBoxControl) component);} else if (component instanceof DateControl) {// 日期框DateControl dateControl = ((DateControl) component);SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy/MM/dd");}else if (component instanceof TreePanelControl) {// 下拉彈出框TreePanelControl treePanelControl = ((TreePanelControl) component);Map map = treePanelControl.getColumnFilters();String elementCode = treePanelControl.getElementCode();String columncode = treePanelControl.getColumncode();if(map.get(elementCode)!=null){Element 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();}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.