dictionary_countryxmlbean.java
来自「java开发的办公系统 1.系统管理 (地区管理,部门管理,菜单管理,用户管理」· Java 代码 · 共 403 行
JAVA
403 行
package com.vere.dictionary.xml;import java.io.*;import java.util.*;import org.jdom.*;import org.jdom.output.*;import org.jdom.input.*;import com.vere.db.*;import com.vere.dictionary.item.*;public class Dictionary_countryXmlBean { private String path=Configuration.getInstance().xmlUpload+"/xmltable/dictionary_country.xml"; public Dictionary_countryXmlBean() { } /** * read XML content. */ public List findXml(){ FileInputStream fi = null; FileWriter fw=null; ArrayList xmllist = null; try{ fi = new FileInputStream(path); xmllist = new ArrayList(); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); //obtain root node List list = root.getChildren(); //obtain child of root node Element element =null; Dictionary_countryItem item =null; for(int i=0;i<list.size();i++){ item = new Dictionary_countryItem(); element = (Element)list.get(i ); //obtain child node informaiton item.setId(element.getChild("id").getText()); item.setCountry_cn_name(element.getChild("country_cn_name").getText()); item.setCountry_en_name(element.getChild("country_en_name").getText()); xmllist.add(item); } } catch(Exception e){ System.err.println(e+"error"); } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } return xmllist; } /** * read XML content. */ public List findXmlItems(String num ){ FileInputStream fi = null; FileWriter fw=null; ArrayList xmllist = null; try{ fi = new FileInputStream(path); xmllist = new ArrayList(); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); //obtain root node List list = root.getChildren(); //obtain child of root node Element element =null; Dictionary_countryItem item =null; for(int i=0;i<list.size();i++){ element = (Element)list.get(i ); //obtain child node informaiton if(element.getChild("num").getText().trim().equals(num)){ item = new Dictionary_countryItem(); element = (Element)list.get(i ); //obtain child node informaiton item.setId(element.getChild("id").getText()); item.setCountry_cn_name(element.getChild("country_cn_name").getText()); item.setCountry_en_name(element.getChild("country_en_name").getText()); xmllist.add(item); } } } catch(Exception e){ System.err.println(e+"error"); } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } return xmllist; } public Dictionary_countryItem findXml(String id){ FileInputStream fi = null; FileWriter fw=null; Dictionary_countryItem item = null; try{ fi = new FileInputStream(path); item = new Dictionary_countryItem(); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); //obtain root node List list = root.getChildren(); //obtain child of root node Element element =null; for(int i=0;i<list.size();i++){ element = (Element)list.get(i ); //obtain child node informaiton if(element.getChild("id").getText().trim().equals(id)){ item.setId(element.getChild("id").getText()); item.setCountry_cn_name(element.getChild("country_cn_name").getText()); item.setCountry_en_name(element.getChild("country_en_name").getText()); return item; } } } catch(Exception e){ System.err.println(e+"error"); } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } return item; } /** * remove element informaiton */ public boolean removeXmlItems(String[] ids){ boolean b=true; FileInputStream fi = null; FileWriter fw=null; try{ fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); Element element =null; List list = root.getChildren(); for(int i=ids.length-1;i>=0;i--){ for(int j=list.size()-1;j>=0;j--){; element = (Element)list.get(j); if(element.getChild("id").getText().trim().equals(ids[i])){ list.remove(j); break; } } } XMLOutputter outer=new XMLOutputter(); fw=new FileWriter(path); Format fm=Format.getPrettyFormat(); fm.setEncoding("gb2312"); outer.setFormat(fm); outer.output(doc,fw); outer.output(doc,System.out); } catch(Exception e){ System.err.println(e+"error"); b=false; } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } return b; } public boolean removeXmlItem(String id){ boolean b=true; FileInputStream fi = null; FileWriter fw=null; try{ fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); List list = root.getChildren(); Element element =null; Dictionary_countryItem item =null; for(int i=0;i<list.size();i++){ element = (Element)list.get(i ); if(element.getChild("id").getText().trim().equals(id)){ list.remove(i); break; } } XMLOutputter outer=new XMLOutputter(); fw=new FileWriter(path); Format fm=Format.getPrettyFormat(); fm.setEncoding("gb2312"); outer.setFormat(fm); outer.output(doc,fw); outer.output(doc,System.out); } catch(Exception e){ System.err.println(e+"error"); b=false; } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } return b; } /** * add a xml information */ public boolean addXmlItem(Dictionary_countryItem item){ FileInputStream fi = null; FileWriter fw=null; boolean b=true; try{ fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); List list = root.getChildren(); Element element =null; Text newtext; Element row= new Element("row"); Element id= new Element("id"); id.setText(item.getId()); row.addContent(id); Element country_cn_name= new Element("country_cn_name"); country_cn_name.setText(item.getCountry_cn_name()); row.addContent(country_cn_name); Element country_en_name= new Element("country_en_name"); country_en_name.setText(item.getCountry_en_name()); row.addContent(country_en_name); list.add(row); XMLOutputter outer=new XMLOutputter(); fw=new FileWriter(path); Format fm=Format.getPrettyFormat(); fm.setEncoding("gb2312"); outer.setFormat(fm); outer.output(doc,fw); outer.output(doc,System.out); } catch(Exception e){ System.err.println(e+"error"); b=false; } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } return b; } /** * add more information **/ public boolean addXmlItems(ArrayList xmlList){ FileInputStream fi = null; FileWriter fw=null; boolean b=true; try{ fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); List list = root.getChildren(); for(int i=0;i<xmlList.size();i++){ Dictionary_countryItem item=(Dictionary_countryItem)xmlList.get(i); Element element =null; Text newtext; Element row= new Element("row"); Element id= new Element("id"); id.setText(item.getId()); row.addContent(id); Element country_cn_name= new Element("country_cn_name"); country_cn_name.setText(item.getCountry_cn_name()); row.addContent(country_cn_name); Element country_en_name= new Element("country_en_name"); country_en_name.setText(item.getCountry_en_name()); row.addContent(country_en_name); list.add(row); } XMLOutputter outer=new XMLOutputter(); fw=new FileWriter(path); Format fm=Format.getPrettyFormat(); fm.setEncoding("gb2312"); outer.setFormat(fm); outer.output(doc,fw); outer.output(doc,System.out); } catch(Exception e){ System.err.println(e+"error"); b=false; } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } return b; } /** * update xml information */ public boolean updateXmlItem(Dictionary_countryItem item){ FileInputStream fi = null; FileWriter fw=null; try{ fi = new FileInputStream(path); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); List list = root.getChildren(); for(int i=0;i<list.size();i++){ Element row=(Element)list.get(i); Element index= row.getChild("id"); if(index.getText().trim().equals(item.getId().trim())){ Element id= row.getChild("id"); id.setText(item.getId_no()); Element country_cn_name= row.getChild("country_cn_name"); country_cn_name.setText(item.getCountry_cn_name()); Element country_en_name= row.getChild("country_en_name"); country_en_name.setText(item.getCountry_en_name()); XMLOutputter outer=new XMLOutputter(); fw=new FileWriter(path); Format fm=Format.getPrettyFormat(); fm.setEncoding("gb2312"); outer.setFormat(fm); outer.output(doc,fw); outer.output(doc,System.out); } } return true; } catch(Exception e){ System.err.println(e+"error"); return false; } finally{ try{ if(fi!=null) fi.close(); if(fw!=null) fw.close(); } catch(Exception e){ e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?