首页 > 代码库 > Dom4j操作xml
Dom4j操作xml
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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。