最近在研究开源软件“局域网聊天工具 Net-C”,项目主页为http://dgtalize.com/products/netc,里面有几段代码是用dom4j来读写xml文件的,本人使用较新的dom4j2.0改写了其中的这段代码。
xml文件为:
config.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- Document : config.xml
- Created on : 19 de marzo de 2008, 22:37
- Author : Diego
- Description: Configuration file por Net-C
- --><config>
- <param>
- <name>ipBroadcast</name>
- <value>192.168.255.255</value>
- </param>
- <param>
- <name>portBroadcast</name>
- <value>41516</value>
- </param>
- <param>
- <name>portPrivateChat</name>
- <value>41518</value>
- </param>
- <param>
- <name>nickname</name>
- <value>robbie</value>
- </param>
- <param>
- <name>trayMsgUserLogged</name>
- <value>true</value>
- </param>
- <param>
- <name>trayMsgUserLoggedOff</name>
- <value>false</value>
- </param>
- <param>
- <name>trayMsgPrivateMsg</name>
- <value>true</value>
- </param>
- <param>
- <name>useNativeUI</name>
- <value>true</value>
- </param>
- <param>
- <name>startMinimized</name>
- <value>false</value>
- </param>
- </config>
得到上述xml文档的Document对象的代码为
- private Document getConfigDocument() throws Exception {
- if (configDoc == null) {
- File cfgFile = new File(filePath);
- if (!cfgFile.exists()) {
- cfgFile.createNewFile();
- try {
- //FileInputStream fis = new FileInputStream(cfgFileOrig);
- InputStream fis = getClass().getResourceAsStream("/com/dgtalize/netc/business/config.xml");
- FileOutputStream fos = new FileOutputStream(cfgFile);
- byte[] buf = new byte[1024];
- int readCant = 0;
- while ((readCant = fis.read(buf)) != -1) {
- fos.write(buf, 0, readCant);
- }
- fis.close();
- fos.close();
- } catch (Exception ex) {
- cfgFile.delete();
- throw ex;
- }
- }
- SAXReader reader = new SAXReader();
- configDoc = reader.read(new File(filePath));
- configDoc.normalize();
- }
- return configDoc;
- }
读取里面的参数(param)元素到变量的代码为:
- public void readConfigFile() throws Exception {
- configDoc = getConfigDocument();
- Element rootElement = configDoc.getRootElement();
- Iterator<Element> it = rootElement.elementIterator("param");
- while (it.hasNext()) {
- Element element = it.next();
- String name = element.elementIterator("name").next().getTextTrim();
- String value = element.elementIterator("value").next().getTextTrim();
- if (name.equalsIgnoreCase("ipBroadcast")) {
- ipBroadcast = value;
- } else if (name.equalsIgnoreCase("portBroadcast")) {
- portBroadcast = Integer.parseInt(value);
- } else if (name.equalsIgnoreCase("nickname")) {
- nickname = value == null ? "" : value;
- } else if (name.equalsIgnoreCase("portPrivateChat")) {
- portPrivateChat = Integer.parseInt(value);
- } else if (name.equalsIgnoreCase("trayMsgUserLogged")) {
- trayMsgUserLogged = Boolean.parseBoolean(value);
- } else if (name.equalsIgnoreCase("trayMsgPrivateMsg")) {
- trayMsgPrivateMsg = Boolean.parseBoolean(value);
- } else if (name.equalsIgnoreCase("trayMsgUserLoggedOff")) {
- trayMsgUserLoggedOff = Boolean.parseBoolean(value);
- } else if (name.equalsIgnoreCase("useNativeUI")) {
- useNativeUI = Boolean.parseBoolean(value);
- } else if (name.equalsIgnoreCase("startMinimized")) {
- startMinimized = Boolean.parseBoolean(value);
- }
- }
- }
根据里面的私有变量保存xml文件的代码为:
- public void saveConfigFile() throws Exception {
- Document configDocForSave = getConfigDocument();
- Element rootElement = configDocForSave.getRootElement();
- Iterator<Element> it = rootElement.elementIterator("param");
- while (it.hasNext()) {
- Element element = it.next();
- Element nextName = element.elementIterator("name").next();
- String name = nextName.getTextTrim();
- Element nextValue = element.elementIterator("value").next();
- String value = nextValue.getTextTrim();
- if (name.equalsIgnoreCase("ipBroadcast")) {
- nextValue.setText(ipBroadcast);
- } else if (name.equalsIgnoreCase("portBroadcast")) {
- nextValue.setText(String.valueOf(portBroadcast));
- } else if (name.equalsIgnoreCase("nickname")) {
- nextValue.setText(nickname);
- } else if (name.equalsIgnoreCase("portPrivateChat")) {
- nextValue.setText(String.valueOf(portPrivateChat));
- } else if (name.equalsIgnoreCase("trayMsgUserLogged")) {
- nextValue.setText(String.valueOf(trayMsgUserLogged));
- } else if (name.equalsIgnoreCase("trayMsgPrivateMsg")) {
- nextValue.setText(String.valueOf(trayMsgPrivateMsg));
- } else if (name.equalsIgnoreCase("trayMsgUserLoggedOff")) {
- nextValue.setText(String.valueOf(trayMsgUserLoggedOff));
- trayMsgUserLoggedOff = Boolean.parseBoolean(value);
- } else if (name.equalsIgnoreCase("useNativeUI")) {
- nextValue.setText(String.valueOf(useNativeUI));
- } else if (name.equalsIgnoreCase("startMinimized")) {
- nextValue.setText(String.valueOf(startMinimized));
- }
- }
- XMLWriter writer = new XMLWriter(new FileWriter(filePath));
- writer.write(configDocForSave);
- writer.close();
- }
转载于:https://blog.51cto.com/robbietree/274904