Dom4j is used to operate XML files and dom4j is used to operate xml files.
1. Create and write 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(); } }
First, create a Document Object with incluenthelper, and then add an Element to the Document, that is, the root Element. Then, you can add several elements under the root Element. Each Element uses addAttribute () the attribute name and attribute value are added. At last, the Document is written to the file using XMLWriter.
2. Read and parse XML
Public static void readXML (String filename) {try {SAXReader saxReader = new SAXReader (); Document document = saxReader. read (filename); // get the root node 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 ();}}
First, create a SAXReader, read a Document using this SAXReader; then obtain the root Element of this Document, iteratively obtain each Element under the root Element, and use attributeValue () for each Element () obtain the property value.
3. Update XML
Public static void updateXML (String filename) {try {File f = new File (filename); SAXReader saxReader = new SAXReader (); Document document Document = saxReader. read (curDir + "\ port_config.xml"); // obtain the root node 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 ();}}
The update process is similar to the read process. You only need to use the setAttributeValue () method to update the attributes to be updated, and then use XMLWriter to write files.
XML file:
<?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>