创建xml
/** *@author Hj, Date: 2011-5-10 * Email: Hj-545@qq.com */ package xml.write; import java.io.File; import java.io.FileWriter; import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class CreateXml { /** * @param args */ public static void main(String[] args) { Document doc = DocumentHelper.createDocument(); Element emRoot = doc.addElement("Data"); Element emRow = emRoot.addElement("row"); Element emSal = emRoot.addElement("sal"); emRow.addAttribute("id", "000001"); emRow.setText("黄坚"); emSal.setText("5000"); emRow = emRoot.addElement("row"); emSal = emRoot.addElement("sal"); emRow.addAttribute("id", "000002"); emRow.setText("hj"); emSal.setText("6000"); try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xw = new XMLWriter( new FileWriter( new File("test.xml")),format); xw.write(doc); xw.close(); } catch (Exception e) { e.printStackTrace(); } } }
删除元素:下面实例只能删除根元素一个元素,元素有重复无法删除
/** *@author Hj, Date: 2011-5-10 * Email: Hj-545@qq.com */ package xml.update; import java.io.File; import java.io.FileWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class UpdateXml { /** * @param args */ public static void main(String[] args) { SAXReader reader = new SAXReader(); String file = "test.xml"; try { Document doc = reader.read(new File(file)); //删除元素,必须先取得上一层元素 List<?> list = doc.selectNodes("/Data"); for(Object obj:list){ Element emData = (Element) obj; Element emSal = emData.element("sal"); emData.remove(emSal); } try { // 设置输出格式,默认createCompactFormat(),不带格式输出 OutputFormat format = OutputFormat.createPrettyPrint(); // 设置输出编码 format.setEncoding("utf-8"); XMLWriter xw = new XMLWriter(new FileWriter(new File(file)), format); xw.write(doc); xw.close(); } catch (Exception e) { e.printStackTrace(); } } catch (DocumentException e) { e.printStackTrace(); } } }
修改元素属性:
/** *@author Hj, Date: 2011-5-10 * Email: Hj-545@qq.com */ package xml.update; import java.io.File; import java.io.FileWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class UpdateXml { /** * @param args */ public static void main(String[] args) { SAXReader reader = new SAXReader(); String file = "test.xml"; try { Document doc = reader.read(new File(file)); // 读取Data根元素下的sal元素 List<?> list = doc.selectNodes("Data/sal"); for (Object obj : list) { Element emSal = (Element) obj; // 获取sal元素的text String strSal = emSal.getText(); // 设置sal为原sal*2 double sal = Double.parseDouble(strSal); emSal.setText("" + (sal * 2)); } //修改元素属性 List<?> alist = doc.selectNodes("Data/row/@id"); Iterator<?> ite = alist.iterator(); while (ite.hasNext()) { Attribute ab = (Attribute) ite.next(); ab.setValue("yes"); } try { // 设置输出格式,默认createCompactFormat(),不带格式输出 OutputFormat format = OutputFormat.createPrettyPrint(); // 设置输出编码 format.setEncoding("utf-8"); XMLWriter xw = new XMLWriter(new FileWriter(new File(file)), format); xw.write(doc); xw.close(); } catch (Exception e) { e.printStackTrace(); } } catch (DocumentException e) { e.printStackTrace(); } } }
更新元素内容
/** *@author Hj, Date: 2011-5-10 * Email: Hj-545@qq.com */ package xml.update; import java.io.File; import java.io.FileWriter; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class UpdateXml { /** * @param args */ public static void main(String[] args) { SAXReader reader = new SAXReader(); String file = "test.xml"; try { Document doc = reader.read(new File(file)); //读取Data根元素下的sal元素 List<?> list = doc.selectNodes("Data/sal"); for (Object obj : list) { Element emSal = (Element) obj; //获取sal元素的text String strSal = emSal.getText(); //设置sal为原sal*2 double sal = Double.parseDouble(strSal); emSal.setText("" + (sal * 2)); } try { //设置输出格式,默认createCompactFormat(),不带格式输出 OutputFormat format = OutputFormat.createPrettyPrint(); //设置输出编码 format.setEncoding("utf-8"); XMLWriter xw = new XMLWriter( new FileWriter( new File(file)),format); xw.write(doc); xw.close(); } catch (Exception e) { e.printStackTrace(); } } catch (DocumentException e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <Data> <row id="000001">hj</row> <sal>9600.0</sal> <row id="000001">ss</row> <sal>400.0</sal> </Data>