程序员人生 网站导航

日常小结-DOM4J解析XML文档

栏目:综合技术时间:2016-07-11 15:40:46

下面为DOM4J解析的方式。DOM4J有教程写的很好可以直接看快速开始。
另外DOM4J通常只需要1个.jar但是仍然有很多支持.jar包,在使用DOM4J的时候如果提示找不到.jar类可以尝试添加支持.jar包

package sax; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; 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; public class Demo1 { @Test public void read() throws DocumentException{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); //读取xml文档第2本书的书名; Element root = document.getRootElement(); Element book = (Element) root.elements("书").get(0); String value = book.element("书名").getText(); System.out.println(value); } @Test public void readatt() throws DocumentException{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); //读取xml文档第2本书的书名; Element root = document.getRootElement(); Element book = (Element) root.elements("书").get(0); String value = book.element("书名").attribute("name").getValue(); System.out.println(value); } @Test public void add() throws DocumentException, IOException{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element book = document.getRootElement().element("书"); book.addElement("售价").setText("209元"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gb2312");//如果要改变编码方式需要1个格式化输出器,否则读入的方式仍然是之前的编码方式,使用字节流,而不能使用字符流,由于字符流会自己查码表,而字节流通过格式化输出器查码表。 //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF⑻")); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"gb2312"),format); writer.write(document); writer.close(); } @Test public void add2() throws DocumentException, IOException{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element book = document.getRootElement().element("书"); List list = book.elements(); Element price = DocumentHelper.createElement("售价"); price.setText("30元"); list.add(2,price); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gb2312");//如果要改变编码方式需要1个格式化输出器,否则读入的方式仍然是之前的编码方式,使用字节流,而不能使用字符流,由于字符流会自己查码表,而字节流通过格式化输出器查码表。 //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF⑻")); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"gb2312"),format); writer.write(document); writer.close(); } @Test public void delete() throws DocumentException, IOException{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element price = document.getRootElement().element("书").element("售价"); price.getParent().remove(price); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gb2312");//如果要改变编码方式需要1个格式化输出器,否则读入的方式仍然是之前的编码方式,使用字节流,而不能使用字符流,由于字符流会自己查码表,而字节流通过格式化输出器查码表。 //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF⑻")); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"gb2312"),format); writer.write(document); writer.close(); } @Test public void update() throws DocumentException, IOException{ SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/book.xml")); Element book = (Element) document.getRootElement().elements("书").get(1); book.element("作者").setText("活拂晓"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gb2312");//如果要改变编码方式需要1个格式化输出器,否则读入的方式仍然是之前的编码方式,使用字节流,而不能使用字符流,由于字符流会自己查码表,而字节流通过格式化输出器查码表。 //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF⑻")); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"gb2312"),format); writer.write(document); writer.close(); } }
<?xml version="1.0" encoding="gb2312"?> <书架> <> <书名 name="xxxxx">java教程</书名> <作者>张孝祥</作者> <售价>39.00元</售价> </> <> <书名>javaScript教程</书名> <作者>活拂晓</作者> <售价>28元</售价> </> </书架>

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐