阿九说:Dom4j解析XML
1.先创建一个info.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <phoneInfo> 2 <brand name="小米"> 3 <type name="小米8"/> 4 <type name="小米9"/> 5 <type name="小米MIX"/> 6 </brand> 7 <brand name="iphone"> 8 <type name="iphone7"/> 9 <type name="iphone8"/> 10 <type name="iphoneX"/> 11 </brand> 12 <brand name="华为"> 13 <type name="华为P30"/> 14 <type name="华为P20"/> 15 <type name="华为P30 pro"/> 16 </brand> 17 </phoneInfo>
2.创建解析XML类Test(解析XML节点及属性的操作)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.kgc.dom4jtest; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.util.Iterator; /** * xml解析 */ public class Test { private SAXReader reader; public static void main(String[] args) throws Exception { SAXReader reader = new SAXReader(); //读取xml文件,并生成document对象,现可通过document来操作文档 Document document = reader.read("src/info.xml"); //获取文档的根节点 Element rootElement = document.getRootElement(); System.out.println("根节点的名字是"+rootElement.getName()); //获取子节点列表 Iterator iterator = rootElement.elementIterator(); while (iterator.hasNext()){ //获取节点的名字 Element firstChild = (Element) iterator.next(); //System.out.println(firstChild.getName()); //获取节点的属性值 System.out.println(firstChild.attribute("name").getValue()); //获取子节点的下一级节点 Iterator iterator1 = firstChild.elementIterator(); while (iterator1.hasNext()){ Element element = (Element) iterator1.next(); System.out.println("\t"+element.attributeValue("name")); } } } }
3.创建添加节点的类AddTest(添加节点及属性的操作)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.kgc.dom4jtest; 2 3 import org.dom4j.Document; 4 import org.dom4j.Element; 5 import org.dom4j.io.SAXReader; 6 import org.dom4j.io.XMLWriter; 7 8 import java.io.File; 9 import java.io.FileOutputStream; 10 import java.io.OutputStream; 11 12 /** 13 * 添加节点的操作 14 */ 15 public class AddTest1 { 16 public static void main(String[] args) throws Exception { 17 SAXReader reader=new SAXReader(); 18 Document document=reader.read("src/info.xml"); 19 //获取到根节点 20 Element rootElement = document.getRootElement(); 21 //添加一个子节点 22 Element newEle = rootElement.addElement("brand"); 23 //给当前节点添加属性 24 newEle.addAttribute("name","魅族"); 25 Element type1 = newEle.addElement("type"); 26 type1.addAttribute("name","s16"); 27 OutputStream o=new FileOutputStream(new File("src/info.xml")); 28 XMLWriter xmlWriter=new XMLWriter(o); 29 xmlWriter.write(rootElement); 30 xmlWriter.flush(); 31 xmlWriter.close(); 32 } 33 }
4.创建修改节点及属性的类UpdateTest(修改节点及属性的操作)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.kgc.dom4jtest; 2 3 import org.dom4j.Document; 4 import org.dom4j.Element; 5 import org.dom4j.io.SAXReader; 6 import org.dom4j.io.XMLWriter; 7 8 import java.io.File; 9 import java.io.FileOutputStream; 10 import java.util.Iterator; 11 12 /** 13 * 执行修改操作 14 */ 15 public class UpdateTest { 16 public static void main(String[] args) throws Exception { 17 SAXReader reader=new SAXReader(); 18 Document document=reader.read("src/info.xml"); 19 //获取根节点 20 Element rootElement = document.getRootElement(); 21 Iterator iterator = rootElement.elementIterator(); 22 while (iterator.hasNext()){ 23 Element firstEle = (Element) iterator.next(); 24 System.out.println(firstEle.attributeValue("name")); 25 if (firstEle.attributeValue("name").equals("魅族")){ 26 Iterator type = firstEle.elementIterator(); 27 while (type.hasNext()){ 28 Element next = (Element) type.next(); 29 if (next.attributeValue("name").equals("s16")){ 30 next.addAttribute("name","16 pro"); 31 } 32 } 33 } 34 } 35 XMLWriter xmlWriter=new XMLWriter(new FileOutputStream(new File("src/info.xml"))); 36 xmlWriter.write(rootElement); 37 xmlWriter.flush(); 38 xmlWriter.close(); 39 } 40 }
5.创建删除节点及属性的类DelTest(删除节点及属性的操作)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.kgc.dom4jtest; 2 3 import org.dom4j.Document; 4 import org.dom4j.Element; 5 import org.dom4j.io.SAXReader; 6 import org.dom4j.io.XMLWriter; 7 8 import java.io.File; 9 import java.io.FileOutputStream; 10 import java.util.Iterator; 11 12 /** 13 * 执行操作 14 */ 15 public class DelTest { 16 public static void main(String[] args) throws Exception { 17 SAXReader saxReader=new SAXReader(); 18 Document document = saxReader.read("src/info.xml"); 19 Element rootElement = document.getRootElement(); 20 Iterator iterator = rootElement.elementIterator(); 21 while (iterator.hasNext()){ 22 Element element = (Element) iterator.next(); 23 if (element.attributeValue("name").equals("魅族")){ 24 //通过父节点删除子节点 25 element.getParent().remove(element); 26 } 27 } 28 XMLWriter writer=new XMLWriter(new FileOutputStream(new File("src/info.xml"))); 29 writer.write(rootElement); 30 writer.flush(); 31 writer.close(); 32 } 33 }