dom4j操作XML

来源:互联网 发布:java list 相同的元素 编辑:程序博客网 时间:2024/06/06 10:42

概述

dom4j提供了很多针对xml的操作,如:读取xml、生成xml、修改xml节点、遍历xml节点、使用xpath进行节点元素搜索定位等。dom4j提供的是一种DOM操作xml的方式,这种DOM操作是将xml以DOM树的结构加载到内存中,然后在内存中再对DOM树进行遍历访问。

以下提供一个操作xml的示例:
package cn.qing.xml.dom4j;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.Node;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;public class Dom4JDemo {/** * @param args */public static void main(String[] args) {Dom4JDemo demo = new Dom4JDemo();String path = "F:\\myEclipseProject\\xmlDemo\\WebRoot\\xmlFolder\\bookInfo.xml";/*boolean result = demo.createXml(path);System.out.println("is Success :"+result);*/demo.parseStringXml(path);}/** * 生成xml文件 * @param path * @return */public boolean createXml(String path){boolean isSuccess = true;//使用DocumentHelper创建Document对象Document document = DocumentHelper.createDocument();document.addComment("这是一个测试xml文档");//已addElement的方式向DOM中添加节点元素,此处的是根节点Element root = document.addElement("books");try {for(int i=0 ; i<5 ; i++){Element book = root.addElement("book");book.addAttribute("id", ""+(i+1));Element bookName = book.addElement("name");bookName.addText("Java 程序设计第"+i+"版");Element bookAuthor = book.addElement("author");bookAuthor.addText("张三");Element bookDate = book.addElement("date");SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String date = sdf.format(new Date());bookDate.addText(date);}//使用格式化的输出OutputFormat format = OutputFormat.createPrettyPrint();//设置xml文件编码格式format.setEncoding("utf-8");//使用XMLWriter将创建的Document对象写入输出流XMLWriter writer = new XMLWriter(new FileOutputStream(new File(path)),format);writer.write(document);writer.close();} catch (Exception e) {e.printStackTrace();isSuccess = false;}System.out.println("xml 文件已生成...");return isSuccess;}/** * 读取xml * @param path */@SuppressWarnings("unchecked")public void readXml(String path){try {//使用SAXReader读取xml,然后在使用Document进行解析SAXReader reader = new SAXReader();Document document = reader.read(new FileInputStream(new File(path)));//可以直接将得到的xml转换成StringString xml = document.asXML();System.out.println(xml);Element root = document.getRootElement();/*Node node = root.selectSingleNode("/books/book[@id='2']/name");System.out.println(node.getText());*/List<Element> list = (List<Element>)root.elements("book");for(Element ele : list){System.out.println(ele.attributeValue("id"));System.out.println(ele.elementTextTrim("name"));System.out.println(ele.elementTextTrim("author"));System.out.println(ele.elementTextTrim("date"));}} catch (Exception e) {e.printStackTrace();}}/** * 使用xpath语法进行搜索定位指定的xml节点 * @param path */public void readXmlByXpath(String path){try {SAXReader reader = new SAXReader();Document document = reader.read(new FileInputStream(new File(path)));Element root = document.getRootElement();// "//book"表达式:选取book子元素,不管它在什么位置List<Node> list = root.selectNodes("//book");System.out.println("list size:"+list.size());for(Node node :  list){System.out.println(node.asXML());}System.out.println("***************firstNode*************");/*  "/books/book[1]" : 选取books元素下的第一个book子元素   */Node firstNode = root.selectSingleNode("/books/book[1]");System.out.println(firstNode.asXML());System.out.println("**************lastNode**************");/*  "/books/book[last()]" : 选取books元素下的最后一个book子元素   */Node lastNode = root.selectSingleNode("/books/book[last()]");System.out.println(lastNode.asXML());System.out.println("**************threeNode**************");/*  "/books/book[position()<=3]" : 选取books元素下的前三个book子元素   */List<Node> threeNode = root.selectNodes("/books/book[position()<=3]");for(Node node : threeNode){System.out.println(node.asXML());}System.out.println("**************idNode**************");/*  "/books/book[@id='2']" : 选取books元素下的属性id=2的book子元素   */Node idNode = root.selectSingleNode("/books/book[@id='2']");System.out.println(idNode.asXML());System.out.println("**************attr**************");/*  "/books/book[@id='3']/name" : 选取books元素下的属性id=3的book子元素的name子元素的值   */Node attr = root.selectSingleNode("/books/book[@id='3']/name");System.out.println(attr.getText());} catch (Exception e) {e.printStackTrace();} }/** * 将XML的字符串格式解析成DOM对象 * @param path */public void parseStringXml(String path){try {SAXReader reader = new SAXReader();Document document = reader.read(new FileInputStream(new File(path)));Element root = document.getRootElement();Node idNode = root.selectSingleNode("/books/book[@id='2']");String stringNode = idNode.asXML();System.out.println(stringNode);System.out.println("*************************************");//将字符串形式的xml解析成Dom对象Document parseDocument = DocumentHelper.parseText(stringNode);System.out.println(parseDocument.asXML());} catch (FileNotFoundException e) {e.printStackTrace();} catch (DocumentException e) {e.printStackTrace();}}}
上面的测试代码可以直接运行测试,通过最后的输出结果可以了解dom4j操作xml常用方法的使用。

ps: 在使用xpath表达式时还需要将jaxen的jar包导入项目中才能正常使用。
0 0
原创粉丝点击