转自:https://www.aliyun.com/jiaocheng/1339446.html
1 xml文件: 2 3 45 19 20 21 用dom4j解析xml文件 22 package com.hfxt; 23 24 import java.io.File; 25 import java.io.FileNotFoundException; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.OutputStreamWriter; 29 import java.util.Iterator; 30 import org.dom4j.Document; 31 import org.dom4j.DocumentException; 32 import org.dom4j.Element; 33 import org.dom4j.io.OutputFormat; 34 import org.dom4j.io.SAXReader; 35 import org.dom4j.io.XMLWriter; 36 37 //dom4j解析xml并且执行(增、删、改、查)操作 38 public class dom4jXmlDemo { 39 40 private Document document; 41 42 public static void main(String[] args){ 43 dom4jXmlDemo domXml = new dom4jXmlDemo(); 44 domXml.getDom(); 45 domXml.addPhoneInfo(); 46 domXml.updatePhoneInfo(); 47 //domXml.deletePhoneInfo(); 48 domXml.showPhoneInfo(); 49 50 } 51 52 //获取DOM树 53 public void getDom(){ 54 SAXReader reader = new SAXReader(); 55 try { 56 document = reader.read(new File("phoneinfo.xml")); 57 } catch (DocumentException e) { 58 e.printStackTrace(); 59 } 60 } 61 62 //增加手机信息 63 public void addPhoneInfo(){ 64 //获取根节点元素 65 Element element = document.getRootElement(); 66 //添加brand节点 67 Element brand = element.addElement("brand"); 68 //为brand添加属性并且赋值 69 brand.addAttribute("name","苹果"); 70 //添加type节点 71 Element type = brand.addElement("type"); 72 Element type1 = brand.addElement("type1"); 73 //为type节点添加name属性并且赋值 74 type.addAttribute("name","iphone7"); 75 type1.addAttribute("name","iphone6"); 76 saveXml("phoneinfo.xml"); 77 } 78 79 //删除手机信息 80 public void deletePhoneInfo(){ 81 //获取根节点 82 Element element = document.getRootElement(); 83 Iterator brand = element.elementIterator(); 84 //遍历获取每个节点信息 85 while(brand.hasNext()){ 86 Element eleBrand = (Element)brand.next(); 87 //删除name为苹果的brand 88 if(eleBrand.attributeValue("name").equals("苹果")){ 89 eleBrand.getParent().remove(eleBrand); 90 } 91 } 92 //保存信息 93 saveXml("phoneinfo.xml"); 94 } 95 96 //修改手机信息 97 public void updatePhoneInfo(){ 98 //获取手机信息 99 Element element = document.getRootElement();100 Iterator brand = element.elementIterator();101 int id = 0;102 while(brand.hasNext()){103 Element eleBrand = (Element)brand.next();104 id++;105 eleBrand.addAttribute("id",id+"");106 }107 saveXml("phoneinfo.xml");108 }109 110 //显示手机信息111 public void showPhoneInfo(){112 //获取根节点113 Element element = document.getRootElement();114 Iterator brand = element.elementIterator();115 while(brand.hasNext()){116 Element eleBrand = (Element)brand.next();117 //根据name属性获取对应的值的名字118 System.out.println(eleBrand.attributeValue("name"));119 //获取type节点120 Iterator type = eleBrand.elementIterator();121 //遍历所有type节点122 while(type.hasNext()){123 Element eleType = (Element)type.next();124 System.out.println("\t"+eleType.attributeValue("name"));125 }126 }127 }128 129 //保存信息到xml130 public void saveXml(String path){131 //格式化132 OutputFormat format = OutputFormat.createPrettyPrint();133 //指定字符编码格式134 format.setEncoding("GBK");135 XMLWriter writer = null;136 //将文件按照指定格式输出到指定路径中137 try {138 writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(path)),format);139 writer.write(document);140 } catch (FileNotFoundException e) {141 e.printStackTrace();142 } catch (IOException e) {143 e.printStackTrace();144 }finally{145 try {146 //关闭服务147 writer.close();148 } catch (IOException e) {149 e.printStackTrace();150 }151 }152 }153 154 }6 107 8 9 11 1412 13 15 1816 17
- 用dom4j解析xml文件并执行增删改查操作
-