码迷,mamicode.com
首页 > 其他好文 > 详细

用dom4j对xml文档解析

时间:2014-11-26 11:34:10      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:xml   java   dom4j   saxreader   

package com.huowolf.dom4j;

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 {
		//读取xml文档的第二本书:<name>安徒生童话</name>
		@Test
		public void read() throws DocumentException {
			SAXReader reader = new SAXReader();
			 Document document = reader.read(new File("books.xml"));
			  
			 Element root = document.getRootElement();
			 Element book = (Element)root.elements("book").get(1);
			 //获取Text
			 String text= book.elementText("name");
			 System.out.println(text);
			 //获取value
			 String value = book.attributeValue("id");
			 System.out.println(value);
			 
		}
		
		//在第一本书上添加一个国籍:<language>English</language>
		@Test
		public void add() throws DocumentException, IOException {
			 SAXReader reader = new SAXReader();
			 Document document = reader.read(new File("books.xml"));
			 
			 Element book = document.getRootElement().element("book");
			 book.addElement("language").setText("English");
			 
			 //输出文件
		      XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("books.xml"),"UTF-8"));
		      /*
		       * 格式化输出器可以指定码表
		       * OutputFormat format = OutputFormat.createPrettyPrint();
		       * format.setEncoding("gb2312");
		       * writer = new XMLWriter( System.out, format );
		       */
		     
		      writer.write( document );
	          writer.close();	          
		}
		
		
		//在第一本书的指定位置上添加一个新的售价:<price>95</price>
		@Test
		public void add2 ()throws Exception{
			 SAXReader reader = new SAXReader();
			 Document document = reader.read(new File("books.xml"));
			 
			 Element book = document.getRootElement().element("book");
			 
			List<Element> list = book.elements();
			 
			 Element price = DocumentHelper.createElement("price");
			 price.setText("95");
			 list.add(3, price);
			 
			 OutputFormat format = OutputFormat.createPrettyPrint();
			 format.setEncoding("UTF-8");
			 
			 XMLWriter writer = new XMLWriter(new FileOutputStream("books.xml"), format);
			 writer.write(document);
			 writer.close();	 
		}
		
		//删除指定节点:<price>89</price>  
		@Test
		public void delete() throws DocumentException, IOException {
			SAXReader reader = new SAXReader();
			 Document document = reader.read(new File("books.xml"));
			 
			 Element price = (Element) document.getRootElement().element("book").elements("price").get(1);
			 price.getParent().remove(price);
			 
			 OutputFormat format = OutputFormat.createPrettyPrint();
			 format.setEncoding("UTF-8");
			 
			 XMLWriter writer = new XMLWriter(new FileOutputStream("books.xml"), format);
			 writer.write(document);
			 writer.close();	 
		}
}

用dom4j对xml文档解析

标签:xml   java   dom4j   saxreader   

原文地址:http://blog.csdn.net/huolang_vip/article/details/41495757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!