Dom4j解析XML

匿名 (未验证) 提交于 2019-12-02 23:49:02

1.Dom4j简介:

        

 创建xml文件:

<?xml version="1.0" encoding="UTF-8"?> <phoneInfo>     <brand name="小米">         <type name="小米8"/>         <type name="小米9"/>         <type name="小米MIX"/>     </brand>     <brand name="iPhone">         <type name="iPhone9"/>         <type name="iPhoneX"/>         <type name="iPhoneXS"/>     </brand>     <brand name="华为">         <type name="P20"/>         <type name="P30"/>         <type name="P30 pro"/>     </brand> </phoneInfo>
info.xml

 xml解析――查询

 1 package cn.kgc.dom4jtest;  2   3 import org.dom4j.Document;  4 import org.dom4j.Element;  5 import org.dom4j.io.SAXReader;  6   7 import java.util.Iterator;  8   9 /** 10  * xml 解析 11  */ 12 public class Test { 13     public static void main(String[] args) { 14         SAXReader saxReader = new SAXReader(); 15         try { 16             //读取xml 文件,并生成document对象,现可通过document来操作文档 17             Document document = saxReader.read("src/info.xml"); 18             //获取到文档的根节点 19             Element rootElement = document.getRootElement(); 20             System.out.println("根节点的名字是"+rootElement.getName()); 21             //获取子节点列表 22             Iterator it = rootElement.elementIterator(); 23             while (it.hasNext()){ 24                 Element fistChild = (Element) it.next(); 25                 //System.out.println(fistChild.getName()); 26                 //获取节点的属性值 27                 System.out.println(fistChild.attribute("name").getValue()); 28                 //获取子节点的下一级节点 29                 Iterator iterator = fistChild.elementIterator(); 30                 while (iterator.hasNext()){ 31                     Element element = (Element) iterator.next(); 32                     System.out.println("\t"+element.attributeValue("name")); 33                 } 34             } 35         } catch (Exception e) { 36             e.printStackTrace(); 37         } 38     } 39 }
Test

  xml解析――添加

 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 AddTest { 16     public static void main(String[] args) { 17         SAXReader saxReader = new SAXReader(); 18         try { 19             Document document = saxReader.read("src/info.xml"); 20             //获取到根节点 21             Element rootElement = document.getRootElement(); 22             //添加一个子节点 23             Element brand = rootElement.addElement("brand"); 24             //给当前节点添加属性 25             brand.addAttribute("name","魅族"); 26             Element type = brand.addElement("type"); 27             type.addAttribute("name","s16"); 28             OutputStream os = new FileOutputStream(new File("src/info.xml")); 29             XMLWriter xmlWriter = new XMLWriter(os); 30             xmlWriter.write(rootElement); 31             xmlWriter.flush(); 32             xmlWriter.close(); 33         } catch (Exception e) { 34             e.printStackTrace(); 35         } 36     } 37 }
AddTest

  xml解析――修改

 1 package cn.kgc.dom4jtest;  2   3 import org.dom4j.Document;  4 import org.dom4j.DocumentException;  5 import org.dom4j.Element;  6 import org.dom4j.io.SAXReader;  7 import org.dom4j.io.XMLWriter;  8   9 import java.io.*; 10 import java.util.Iterator; 11  12 /** 13  * 节点修改测试 14  */ 15 public class UpdateTest { 16     public static void main(String[] args) { 17         SAXReader saxReader = new SAXReader(); 18         try { 19             Document document = saxReader.read("src/info.xml"); 20             //获取根节点 21             Element rootElement = document.getRootElement(); 22             //获取子节点 23             Iterator it = rootElement.elementIterator(); 24             while (it.hasNext()){ 25                 Element element = (Element) it.next(); 26                 System.out.println(element.attributeValue("name")); 27                 if (element.attributeValue("name").equals("魅族")){ 28                     Iterator iterator = element.elementIterator(); 29                     while (iterator.hasNext()){ 30                         Element type = (Element) iterator.next(); 31                         if (type.attributeValue("name").equals("s16")){ 32                             type.addAttribute("name","16 pro"); 33                         } 34                     } 35                 } 36             } 37             XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File("src/info.xml"))); 38             xmlWriter.write(document); 39             xmlWriter.flush(); 40             xmlWriter.close(); 41         } catch (Exception e) { 42             e.printStackTrace(); 43         } 44  45     } 46 }
UpdateTest

  xml解析――删除

 1 package cn.kgc.dom4jtest;  2   3 import org.dom4j.Document;  4 import org.dom4j.DocumentException;  5 import org.dom4j.Element;  6 import org.dom4j.io.SAXReader;  7 import org.dom4j.io.XMLWriter;  8   9 import java.io.File; 10 import java.io.FileOutputStream; 11 import java.util.Iterator; 12  13 /** 14  * 删除节点 15  */ 16  17 public class DelTest { 18     public static void main(String[] args) { 19         SAXReader saxReader = new SAXReader(); 20         try { 21             Document document = saxReader.read("src/info.xml"); 22             Element rootElement = document.getRootElement(); 23             Iterator iterator = rootElement.elementIterator(); 24             while (iterator.hasNext()){ 25                 Element brand = (Element) iterator.next(); 26                 if(brand.attributeValue("name").equals("魅族")){ 27                     //通过父节点来删除子节点 28                     brand.getParent().remove(brand); 29                 } 30             } 31             XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File("src/info.xml"))); 32             xmlWriter.write(document); 33             xmlWriter.flush(); 34             xmlWriter.close(); 35         } catch (Exception e) { 36             e.printStackTrace(); 37         } 38     } 39 }
DelTest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!