首页 > 代码库 > dom4j的小例子
dom4j的小例子
1.要解析的xml文件book.xml
<?xml version="1.0" encoding="UTF-8"?><books> <book id="book1"> <title>JAVA编程思想</title> <price>80.00</price> </book> <book id="book2"> <title>JAVA核心技术</title> <price>100.00</price> </book></books>
2.解析xml文件
package com.chen.dom;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.OutputStreamWriter;import java.util.Iterator;import java.util.List;import org.dom4j.Attribute;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;public class Dom4jTest { @Test public void read() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("resource/book.xml")); Element root = document.getRootElement(); Element book1 = (Element) root.elements("book").get(0); String id1 = book1.attributeValue("id"); String titleText = book1.element("title").getTextTrim(); System.out.println(id1 + "---" + titleText); } /* * 将整个xml使用递归完整输出 */ @Test public void readIterator() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("resource/book.xml")); Element root = document.getRootElement(); // 递归解析xml parse(root); } // 递归解析xml public void parse(Element element) { System.out.print("<" + element.getName()); // 遍历节点的属性 List<Attribute> attributes = element.attributes(); for (int i = 0; i < attributes.size(); i++) { Attribute attr = attributes.get(i); String name = attr.getName(); String value = attr.getValue(); System.out.print(" " + name + "=\"" + value + "\""); } System.out.println(">"); // 获取节点的文本信息 if (element.getTextTrim() != null && !"".equals(element.getTextTrim())) { System.out.println(element.getTextTrim()); } // 对该节点进行递归 for (Iterator i = element.elementIterator(); i.hasNext();) { parse((Element) i.next()); } System.out.println("</" + element.getName() + ">"); } @Test public void add() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("resource/book.xml")); Element root = document.getRootElement(); Element book1 = (Element) root.elements("book").get(1); Element authorElement = book1.addElement("author") .addAttribute("id", "author2").addText("张三"); OutputFormat format = OutputFormat.createPrettyPrint(); //从这里可以看出dom4j默认的编码方式为utf-8 System.out.println(format.getEncoding()); //设置编码格式防止乱码 <?xml version="1.0" encoding="gb2312"?> format.setEncoding("utf-8"); //使用FileWriter会乱码,因为使用了默认的gb2312编码输出,而dom4j使用的是默认的UTF-8编码 //如果format.setEncoding("gb2312"); 使用FileWriter就不会乱码 //XMLWriter writer = new XMLWriter(new FileWriter("resource/book.xml"),format); //使用FileOutputStream输出时指定编码 这个编码要和format.setEncoding("utf-8"); 一样 //XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("resource/book.xml"), "utf-8"),format); //最简单的方法是设置format.setEncoding("utf-8");然后使用字节流输出 XMLWriter writer = new XMLWriter(new FileOutputStream("resource/book.xml"),format); writer.write(document); writer.close(); } //在指定位置上添加个新节点(改变list集合的顺序) @Test public void add2() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("resource/book.xml")); Element element = document.getRootElement().element("book"); List list = element.elements(); Element publishElement = DocumentHelper.createElement("publish"); publishElement.addText("机械工业出版社"); //改变list集合的顺序,就可以达到在指定位置添加新节点 list.add(1, publishElement); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("resource/book.xml"),format); writer.write(document); writer.close(); }}
xml乱码的样子:
dom4j的小例子
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。