导航

进入黑马day2-解析xml三种方法(3)dom4j解析器

Posted on 2013-02-25 20:41  酷鱼影子  阅读(159)  评论(0编辑  收藏  举报

package cn.itheima.dom4j;

 

import java.io.FileWriter;

importjava.io.IOException;

importjava.util.List;

 

importorg.dom4j.Document;

importorg.dom4j.DocumentHelper;

importorg.dom4j.Element;

importorg.dom4j.io.OutputFormat;

importorg.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

import org.junit.Test;

 

public class Demo3 {

       //增加元素

       @Test

       publicvoid add() throws Exception {

              SAXReaderread = new SAXReader();

              Documentdom = read.read("book.xml");

              //在第一本书增加<特价>

              ElementbookEle = dom.getRootElement().element("书");

 

              //用DocumentHelper可以创建元素,并设置标签体为10.0元

              Elementpri2Ele = DocumentHelper.createElement("特价");

              pri2Ele.setText("10.0元");

              bookEle.add(pri2Ele);

 

              ListbookList = dom.getRootElement().elements();

              //dem4j获取标签的时候帮我们排除了空白标签

              ElementbookEle = (Element) bookList.get(0);

              StringbookName = bookEle.element("书名").getText();

              System.out.println(bookName);

              //修改元素标签体

              Element  pri2Ele = dom.getRootElement().element("书").element("特价");

              pri2Ele.setText("9.9元");

             

              //删除标签

              Element  bookEle = dom.getRootElement().element("书");

              Elementpri2Ele = bookEle.element("特价");

              bookEle.remove(pri2Ele);

              //添加属性

              Element  bookEle = dom.getRootElement().element("书");

              bookEle.addAttribute("出版社", "人民日报");

             

              //更新XML文件

              //XMLWriter默认是UTF-8的,所以必须重设输出编码

              OutputFormatformat =OutputFormat.createPrettyPrint();

              format.setEncoding("gb2312");

              XMLWriterwriter = new XMLWriter(new FileWriter("book.xml"),format);

              writer.write(dom);

              writer.close();

       }

 

       附:利用dom4j中的DocumentHelper类创建xml文件

       @Test

       publicvoid creatDocument() throws Exception{

              Documentdom = DocumentHelper.createDocument();

              Elementroot= dom.addElement("书架");

              Elementbook1 = root.addElement("书").addAttribute("出版社", "中信出版社").addAttribute("编号", "110");

              book1.addElement("作者").addText("郎咸平");

              book1.addElement("书名").addText("货币战争");

              book1.addElement("价格").addText("32.0元");

              Elementbook2 = root.addElement("书").addAttribute("出版社", "人民出版社").addAttribute("编号", "111");

              book2.addElement("作者").addText("柴静");

              book2.addElement("书名").addText("遇见");

              book2.addElement("价格").addText("38.0元");

              //将输入格式设置gb2312编码格式

              OutputFormatformat =OutputFormat.createPrettyPrint();

              format.setEncoding("gb2312");

              XMLWriterwriter = new XMLWriter(new FileWriter("book1.xml"),format);

              writer.write(dom);

              writer.close();

       }

}