最近在研究开源软件“局域网聊天工具 Net-C”,项目主页为http://dgtalize.com/products/netc,里面有几段代码是用dom4j来读写xml文件的,本人使用较新的dom4j2.0改写了其中的这段代码。

xml文件为:

config.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!-- 
  3.     Document   : config.xml 
  4.     Created on : 19 de marzo de 2008, 22:37 
  5.     Author     : Diego 
  6.     Description: Configuration file por Net-C 
  7. --><config> 
  8.     <param> 
  9.         <name>ipBroadcast</name> 
  10.         <value>192.168.255.255</value> 
  11.     </param> 
  12.     <param> 
  13.         <name>portBroadcast</name> 
  14.         <value>41516</value> 
  15.     </param> 
  16.     <param> 
  17.         <name>portPrivateChat</name> 
  18.         <value>41518</value> 
  19.     </param> 
  20.     <param> 
  21.         <name>nickname</name> 
  22.         <value>robbie</value> 
  23.     </param> 
  24.     <param> 
  25.         <name>trayMsgUserLogged</name> 
  26.         <value>true</value> 
  27.     </param> 
  28.     <param> 
  29.         <name>trayMsgUserLoggedOff</name> 
  30.         <value>false</value> 
  31.     </param> 
  32.     <param> 
  33.         <name>trayMsgPrivateMsg</name> 
  34.         <value>true</value> 
  35.     </param> 
  36.     <param> 
  37.         <name>useNativeUI</name> 
  38.         <value>true</value> 
  39.     </param> 
  40.     <param> 
  41.         <name>startMinimized</name> 
  42.         <value>false</value> 
  43.     </param> 
  44. </config> 

得到上述xml文档的Document对象的代码为

 


  
  1. private Document getConfigDocument() throws Exception { 
  2.         if (configDoc == null) { 
  3.             File cfgFile = new File(filePath); 
  4.             if (!cfgFile.exists()) { 
  5.  
  6.                 cfgFile.createNewFile(); 
  7.                 try { 
  8.                     //FileInputStream fis = new FileInputStream(cfgFileOrig); 
  9.                     InputStream fis = getClass().getResourceAsStream("/com/dgtalize/netc/business/config.xml"); 
  10.                     FileOutputStream fos = new FileOutputStream(cfgFile); 
  11.                     byte[] buf = new byte[1024]; 
  12.                     int readCant = 0
  13.                     while ((readCant = fis.read(buf)) != -1) { 
  14.                         fos.write(buf, 0, readCant); 
  15.                     } 
  16.                     fis.close(); 
  17.                     fos.close(); 
  18.                 } catch (Exception ex) { 
  19.                     cfgFile.delete(); 
  20.                     throw ex; 
  21.                 } 
  22.             } 
  23.  
  24.             SAXReader reader = new SAXReader(); 
  25.             configDoc = reader.read(new File(filePath)); 
  26.             configDoc.normalize(); 
  27.         } 
  28.         return configDoc; 
  29.     } 

读取里面的参数(param)元素到变量的代码为:

 


  
  1. public void readConfigFile() throws Exception { 
  2.         configDoc = getConfigDocument(); 
  3.         Element rootElement = configDoc.getRootElement(); 
  4.         Iterator<Element> it = rootElement.elementIterator("param"); 
  5.         while (it.hasNext()) { 
  6.             Element element = it.next(); 
  7.             String name = element.elementIterator("name").next().getTextTrim(); 
  8.             String value = element.elementIterator("value").next().getTextTrim(); 
  9.             if (name.equalsIgnoreCase("ipBroadcast")) { 
  10.                 ipBroadcast = value; 
  11.             } else if (name.equalsIgnoreCase("portBroadcast")) { 
  12.                 portBroadcast = Integer.parseInt(value); 
  13.             } else if (name.equalsIgnoreCase("nickname")) { 
  14.                 nickname = value == null ? "" : value; 
  15.             } else if (name.equalsIgnoreCase("portPrivateChat")) { 
  16.                 portPrivateChat = Integer.parseInt(value); 
  17.             } else if (name.equalsIgnoreCase("trayMsgUserLogged")) { 
  18.                 trayMsgUserLogged = Boolean.parseBoolean(value); 
  19.             } else if (name.equalsIgnoreCase("trayMsgPrivateMsg")) { 
  20.                 trayMsgPrivateMsg = Boolean.parseBoolean(value); 
  21.             } else if (name.equalsIgnoreCase("trayMsgUserLoggedOff")) { 
  22.                 trayMsgUserLoggedOff = Boolean.parseBoolean(value); 
  23.             } else if (name.equalsIgnoreCase("useNativeUI")) { 
  24.                 useNativeUI = Boolean.parseBoolean(value); 
  25.             } else if (name.equalsIgnoreCase("startMinimized")) { 
  26.                 startMinimized = Boolean.parseBoolean(value); 
  27.             } 
  28.         } 
  29.  
  30.     } 

根据里面的私有变量保存xml文件的代码为:

 


  
  1. public void saveConfigFile() throws Exception { 
  2.         Document configDocForSave = getConfigDocument(); 
  3.  
  4.         Element rootElement = configDocForSave.getRootElement(); 
  5.         Iterator<Element> it = rootElement.elementIterator("param"); 
  6.         while (it.hasNext()) { 
  7.             Element element = it.next(); 
  8.             Element nextName = element.elementIterator("name").next(); 
  9.             String name = nextName.getTextTrim(); 
  10.             Element nextValue = element.elementIterator("value").next(); 
  11.             String value = nextValue.getTextTrim(); 
  12.             if (name.equalsIgnoreCase("ipBroadcast")) { 
  13.                 nextValue.setText(ipBroadcast); 
  14.             } else if (name.equalsIgnoreCase("portBroadcast")) { 
  15.                 nextValue.setText(String.valueOf(portBroadcast)); 
  16.             } else if (name.equalsIgnoreCase("nickname")) { 
  17.                 nextValue.setText(nickname); 
  18.             } else if (name.equalsIgnoreCase("portPrivateChat")) { 
  19.                 nextValue.setText(String.valueOf(portPrivateChat)); 
  20.             } else if (name.equalsIgnoreCase("trayMsgUserLogged")) { 
  21.                 nextValue.setText(String.valueOf(trayMsgUserLogged)); 
  22.             } else if (name.equalsIgnoreCase("trayMsgPrivateMsg")) { 
  23.                 nextValue.setText(String.valueOf(trayMsgPrivateMsg)); 
  24.             } else if (name.equalsIgnoreCase("trayMsgUserLoggedOff")) { 
  25.                 nextValue.setText(String.valueOf(trayMsgUserLoggedOff)); 
  26.                 trayMsgUserLoggedOff = Boolean.parseBoolean(value); 
  27.             } else if (name.equalsIgnoreCase("useNativeUI")) { 
  28.                 nextValue.setText(String.valueOf(useNativeUI)); 
  29.             } else if (name.equalsIgnoreCase("startMinimized")) { 
  30.                 nextValue.setText(String.valueOf(startMinimized)); 
  31.             } 
  32.         } 
  33.         XMLWriter writer = new XMLWriter(new FileWriter(filePath)); 
  34.         writer.write(configDocForSave); 
  35.         writer.close(); 
  36.  
  37.     }