Dom4j操作XML文件
Posted 代码空间
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dom4j操作XML文件相关的知识,希望对你有一定的参考价值。
1. 创建并写入XML
public static void writeXML(String filename){ try { Document document = DocumentHelper.createDocument(); Element root = document.addElement("PortConfig"); Element ePort; ePort = root.addElement("Port"); ePort.addAttribute("Portname", "portBloodpress"); ePort.addAttribute("Portnumber", "COM12"); ePort = root.addElement("Port"); ePort.addAttribute("Portname", "portWah"); ePort.addAttribute("Portnumber", "COM14"); ePort = root.addElement("Port"); ePort.addAttribute("Portname", "portBloodsugar"); ePort.addAttribute("Portnumber", "COM16"); ePort = root.addElement("Port"); ePort.addAttribute("Portname", "portBloodfat"); ePort.addAttribute("Portnumber", "COM18"); ePort = root.addElement("Port"); ePort.addAttribute("Portname", "portUric"); ePort.addAttribute("Portnumber", "COM20"); OutputFormat format = new OutputFormat(" ", true); XMLWriter output = new XMLWriter(new FileWriter(new File(filename)), format); output.write(document); output.close(); } catch (Exception e) { e.printStackTrace(); } }
首先用DocumentHelper创建一个Document对象,然后给Document添加一个Element,也就是根Element;然后可以在这个根Element下面添加若干个Element,每一个Element用addAttribute()添加了属性名称和属性值;最后,使用XMLWriter把Document写入文件。
2. 读取并解析XML
public static void readXML(String filename){ try { SAXReader saxReader = new SAXReader(); Document document = saxReader.read(filename); // 获取根节点 Element portConfig = document.getRootElement(); for (Iterator i = portConfig.elementIterator(); i.hasNext();) { Element port = (Element) i.next(); String aname = port.attributeValue("Portname"); String avalue = port.attributeValue("Portnumber"); if (aname.equals("portBloodpress")){ Constant.portBloodpress = avalue; } else if (aname.equals("portWah")){ Constant.portWah = avalue; } else if (aname.equals("portBloodsugar")){ Constant.portBloodsugar = avalue; } else if (aname.equals("portBloodfat")){ Constant.portBloodfat = avalue; } else if (aname.equals("portUric")){ Constant.portUric = avalue; } } } catch (Exception e) { e.printStackTrace(); } }
首先创建一个SAXReader,用这个SAXReader读出一个Document;然后获得这个Document的根Element,迭代获得根Element下面的每一个Element,对每一个Element,使用attributeValue()获得它的属性值。
3. 更新XML
public static void updateXML(String filename){ try { File f = new File(filename); SAXReader saxReader = new SAXReader(); Document document = saxReader.read(curDir + "\\port_config.xml"); // 获取根节点 Element portConfig = document.getRootElement(); for (Iterator i = portConfig.elementIterator(); i.hasNext();) { Element port = (Element) i.next(); String aname = port.attributeValue("Portname"); if (aname.equals("portBloodpress")){ port.setAttributeValue("Portnumber", Constant.portBloodpress); } else if (aname.equals("portWah")){ port.setAttributeValue("Portnumber", Constant.portWah); } else if (aname.equals("portBloodsugar")){ port.setAttributeValue("Portnumber", Constant.portBloodsugar); } else if (aname.equals("portBloodfat")){ port.setAttributeValue("Portnumber", Constant.portBloodfat); } else if (aname.equals("portUric")){ port.setAttributeValue("Portnumber", Constant.portUric); } } XMLWriter output = new XMLWriter(new FileWriter(new File(curDir + "\\port_config.xml"))); output.write(document); output.close(); } catch (Exception e) { e.printStackTrace(); } }
更新和读取的过程差不多,只是对需要更新的属性用setAttributeValue()方法更新,最后再用XMLWriter写入文件。
XML文件:
<?xml version="1.0" encoding="UTF-8"?> <PortConfig> <Port Portname="portBloodpress" Portnumber="COM126"/> <Port Portname="portWah" Portnumber="COM146"/> <Port Portname="portBloodsugar" Portnumber="COM166"/> <Port Portname="portBloodfat" Portnumber="COM186"/> <Port Portname="portUric" Portnumber="COM206"/> </PortConfig>
以上是关于Dom4j操作XML文件的主要内容,如果未能解决你的问题,请参考以下文章