import java.awt.List; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; /* * DOM4J方法解析xml */ /* * 思路: * 第一步:获得解析器 * 第二步:解析器要读取那个文档 * 第三步:读取了xml文档准备进行什么操作(增删改查) * 获得根元素进行操作,操作都要先获得根节点document.getRootElement(); * 然后根据根节点的root.element("elementName")表示下一个子节点,可以 * root.element("name").element(name)一直往下读取 * 如果是第二本书root.element("elementName").get(1);进行操作 * 增加节点用addElement()方法,documentHelper.createElement() */ /* <?xml version="1.0" encoding="UTF-8"?> <BookStore> <myBook NO="1"> <Book> <BookName ID="A001">JAVA</BookName> <love>I love you</love> <Author>Jack</Author> <Price>110</Price> <City>nanjing</City> </Book> <Book> <BookName ID="A002">android</BookName> <Author>Lucy</Author> <Price>20</Price> </Book> </myBook> </BookStore> */ public class DOM4J { @Test public void readContent()throws Exception { //读取第二本书的书名<BookName ID="A002">android</BookName> SAXReader reader=new SAXReader(); Document d=reader.read(new File("src/book.xml")); //先得到根节点<BookStore>,在<Book>,在<BookName> Element root=d.getRootElement(); Element element= (Element) root.element("myBook").elements().get(1); System.out.println(element.element("BookName").getText()); } @Test public void readAttr()throws Exception { //读取第二本书的书名的属性ID<BookName ID="A002">android</BookName> //先获得解析器 SAXReader reader =new SAXReader(); Document document= reader.read(new File("src/book.xml")); //获得第二本书的属性,先获得根节点,再获得子节点的 Element root= document.getRootElement(); Element element=(Element) root.element("myBook").elements("Book").get(1); String str=element.element("BookName").attributeValue("ID"); System.out.println(str); } @Test public void add()throws Exception { //往第一本书上加入<City>nanjing</City> SAXReader reader=new SAXReader(); Document document=reader.read(new File("src/book.xml")); Element rootElement= document.getRootElement(); Element book= rootElement.element("myBook").element("Book");//获取到了book的节点 book.addElement("City").setText("nanjing"); //增加节点后,要保存进xml内存中去 OutputFormat format=OutputFormat.createPrettyPrint();//格式化xml代码 XMLWriter writer=new XMLWriter(new FileWriter("src/book.xml"), format); writer.write(document);//讲document写入writer,writer在写入src/book.xml中 writer.close(); } @Test public void addBySomeWhere()throws Exception { //往指定位置添加,更改保存所有孩子的list集合的顺序 //往第一本书第二个位置加入<love>I love you</love> SAXReader reader=new SAXReader(); Document document=reader.read(new File("src/book.xml")); //先获得根节点下的孩子集合list[BookName],[author],[price],[city] java.util.List list=document.getRootElement().element("myBook").element("Book").elements(); //创建节点 Element element=DocumentHelper.createElement("love"); element.setText("I love you"); list.add(1, element);//把该节点放在第二个位置上去 //增加节点后,要保存进xml内存中去 OutputFormat format=OutputFormat.createPrettyPrint();//格式化xml代码 XMLWriter writer=new XMLWriter(new FileWriter("src/book.xml"), format); writer.write(document);//讲document写入writer,writer在写入src/book.xml中 writer.close(); } @Test public void delete()throws Exception { //删除第二本书的<Price>20</Price> SAXReader reader=new SAXReader(); Document document=reader.read(new File("src/book.xml")); Element element=(Element) document.getRootElement().element("myBook").elements("Book").get(1); Element child= element.element("Price"); element.remove(child); XMLWriter writer=new XMLWriter(new FileOutputStream("src/book.xml"), OutputFormat.createPrettyPrint()); writer.write(document); writer.close(); } @Test public void mytest()throws Exception { //遍历节点 SAXReader reader=new SAXReader(); Document document=reader.read(new File("src/book.xml")); Element rootElement=document.getRootElement(); Iterator tIterator=rootElement.elementIterator(); while(tIterator.hasNext()) { Element myeElement=(Element) tIterator.next(); System.out.println(myeElement.getName()); } } }