JAVA操作XML文件 - dom4j操作

操作前,记得加载dom4j的jar文件


示例代码如下

package web_day04;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import java.util.List;

/*
 * 读取节点
 * 读取属性
 * 添加节点
 * 删除节点
 * 更新节点
 * 打印元素节点
 * 
 */
 

public class Dom4jExercises {
	//读取节点文本
	@Test
	public void readNodeText() throws DocumentException{
		//获得代表xml文件的document对象
		Document document = getDomDocment();
		Element rootElement = document.getRootElement();
		Element firstBookElement = rootElement.element("书");
		Element firstBookNameElement = firstBookElement.element("书名");
		String bookName = firstBookNameElement.getText();
		System.out.println(bookName);
		
	}
	
	//读取所有属性的值
	@Test
	public void testReadAttribute() throws DocumentException{
		Document document = getDomDocment();
		//获得第二本书的属性
		Element rootElement = document.getRootElement();
		List listElement = rootElement.elements();
		
		for(int i=0;i<listElement.size();i++)
		{
			//获取子元素的属性
			//listElement.get(i).toString();
			//System.out.println(listElement.get(i).toString());
			Element nodeElement = (Element)listElement.get(i);
			String attrStr =  nodeElement.attributeValue("出版社");
			System.out.println(attrStr);
		}
		
	}
	//添加节点 [第二条加入]
	@Test
	public void addNodeTest() throws IOException, DocumentException{
		Document document = getDomDocment();
		
		//第二条添加一个price
		
		Element rootElement = document.getRootElement();
		List listElement = rootElement.elements();
		Element secondElement = (Element)listElement.get(1);
		
		secondElement.addElement("price").addText("7.8折");
		
		//写会文件
		//document.setXMLEncoding("utf-8");
		
		write(document,"utf-8");
	}
	
	@Test
	public void deleteNode() throws DocumentException, IOException{
		Document document = getDomDocment();
		Element rootElement = document.getRootElement();
		List listElement = rootElement.elements();
		
		Element secondElement = (Element)listElement.get(1);
		secondElement.remove((Element)(secondElement.elements().get(3)));
		
		write(document,"utf-8");
		
		
	}
	
	
	
	private Document getDomDocment() throws DocumentException {
		SAXReader reader = new SAXReader();
        Document document = reader.read("src/book.xml");
        return document;
	}
	
	
	public void write(Document document,String encode) throws IOException {
		

        //注意由于FileWriter 很久没有更新,一致使用的是老版本的
        //FileWriter始终使用的是当前系统相关的编码,跟自己设置的编码无关
        //原则写文件,不要使用FileWriter
		
		
		OutputFormat format = new OutputFormat();
		
		if (null != encode)  
        {  
            format.setEncoding(encode.toUpperCase());  
        }  
		
        // lets write to a file
		/*
        XMLWriter writer = new XMLWriter(
            new FileWriter( "src/book.xml" ),format
        );
        */
		XMLWriter writer = new XMLWriter(
				new FileOutputStream("src/book.xml"),format
				);
        
        writer.write( document );
        writer.close();
    }
}

原文链接: JAVA操作XML文件 - dom4j操作 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-34.html )

发表评论

0则评论给“JAVA操作XML文件 - dom4j操作”