dom4j 的使用
(一)准备的包
dom4j-1.6.1.jar
(二)读取配置文件的关键代码如下
private synchronized static void loadUiMap() { try { String path = XmlUtil.class.getClassLoader().getResource("").toURI() .getPath() + orderXmlName; File file = new File(path); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(file); Element root1 = document.getRootElement(); Element root = root1.element("ui"); CLIPBOARD_START_TIME = root.elementText("CLIPBOARD_START_TIME");
(三)写配置文件的关键代码如下:
String toConfigText = jTextArea.getText(); // 先保存起来 try { Document document = DocumentHelper .parseText(toConfigText); XMLWriter writer = new XMLWriter( new FileOutputStream(path)); writer.write(document); writer.close();
(四)一个修改版本号将版本号加一的代码
// 将xml客户端文件的版本号加0.1 public synchronized void alertClentXmlVersion() { try { String path = getClass().getClassLoader().getResource("").toURI().getPath() + XmlUtil.orderXmlName; File file = new File(path); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(file); Element root = document.getRootElement(); Element element = root.element("clent"); Element element2 = element.element("CLENTVERSION"); float version = Float.parseFloat(element2.getTextTrim()) + 0.1F; DecimalFormat fnum = new DecimalFormat("##0.0"); String versionStr = fnum.format(version); element2.setText(versionStr); XMLWriter writer = new XMLWriter(new FileOutputStream(path)); writer.write(document); writer.close(); } catch (Exception e) { log.info(e); } }