你的位置:首页 > Java教程

[Java教程]Dom4j操作XML文件

1. 创建并写入

  public static void writetry {      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);      = new 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()添加了属性名称和属性值;最后,使用

2. 读取并解析

  public static void readtry {             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. 更新

  public static void updatetry {             File f = new File(filename);      SAXReader saxReader = new SAXReader();      Document document = saxReader.read(curDir + "\\port_config.);      // 获取根节点      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);        }      }      = new new FileWriter(new File(curDir + "\\port_config.)));      output.write(document);      output.close();    } catch (Exception e) {      e.printStackTrace();    }      }

更新和读取的过程差不多,只是对需要更新的属性用setAttributeValue()方法更新,最后再用

<??><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>