1. 程式人生 > >Dom4j迴圈讀取xml節點

Dom4j迴圈讀取xml節點

進期在做資料處理,遇到使用Dom4j解析檔案,記錄demo備用。

需求:迴圈出多個同一父節點下多個字位元組點的資料,然後把這些資料按照順序重新生成新的放入到新的節點名不同的xml內。

準備:dom4j-1.6.1.jar、原始xml檔案

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <edition>1.0</edition>
    <copyright>巴倫創維</copyright>
    <article>
        <資訊標題>資訊標題1</資訊標題>
	<內容描述>內容描述1</內容描述>
        <資訊內容>資訊內容1</資訊內容>
        <建立日期>建立日期</建立日期>
        <成文日期>建立日期</成文日期>
    </article>
    <article>
        <資訊標題>資訊標題2</資訊標題>
	<內容描述>內容描述2</內容描述>
        <資訊內容>資訊內容2</資訊內容>
        <建立日期>建立日期2</建立日期>
        <成文日期>成文日期2</成文日期>
    </article>
    <article>
        <資訊標題>資訊標題3</資訊標題>
	<內容描述>內容描述3</內容描述>
        <資訊內容>資訊內容3</資訊內容>
        <建立日期>建立日期3</建立日期>
        <成文日期>成文日期3</成文日期>
    </article>
    <article>
        <資訊標題>資訊標題4</資訊標題>
	<內容描述>內容描述4</內容描述>
        <資訊內容>資訊內容4</資訊內容>
        <建立日期>建立日期4</建立日期>
        <成文日期>成文日期4</成文日期>
    </article>
    <article>
        <資訊標題>資訊標題5</資訊標題>
	<內容描述>內容描述5</內容描述>
        <資訊內容>資訊內容5</資訊內容>
        <建立日期>成文日期5</建立日期>
        <成文日期>成文日期5</成文日期>
    </article>
    <article>
        <資訊標題>資訊標題6</資訊標題>
	<內容描述>內容描述6</內容描述>
        <資訊內容>資訊內容6</資訊內容>
        <建立日期>成文日期6</建立日期>
        <成文日期>成文日期6</成文日期>
    </article>
</book>

直接貼程式碼:

package mysqlxml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 
 * 資料處理,xml轉換節點名稱
 * @author baron
 *
 */
public class Dom4jDemo1 {
	
    /**
     * 轉換xml節點名稱
     * @param node 根節點
     */
    public void exportNewXML(Element node){
	String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    	xml+="\r\n";
    	xml+="<book>";
    	xml+="\r\n";
    	xml+="\t"+"<edition>1.0</edition>";
    	xml+="\r\n";
    	xml+="\t"+"<copyright>巴倫創維</copyright>";
    	xml+="\r\n";
	//獲取子節點集合
    	List<Element> elarts = node.elements();
    	
    	try {
    		//迴圈
    		for (Element ele : elarts) {
    			//獲取article子節點的資料跟xml拼接,生成新的xml檔案
    			if(ele.getName().equals("article")){
    				xml+="\t"+"<article> "+"\r\n";
		        	xml+="\t"+"\t"+"<title>"+ ele.element("資訊標題").getText() +"</title>" +"\r\n";
		        	xml+="\t"+"\t"+"<crtime>"+ ele.element("建立日期").getText() +"</crtime>" +"\r\n";
		        	xml+="\t"+"\t"+"<reltime>"+ ele.element("成文日期").getText() +"</reltime>" +"\r\n";
		        	xml+="\t"+"\t"+"<depiction>"+ ele.element("內容描述").getText() +"</depiction>" +"\r\n";
		        	xml+="\t"+"\t"+"<text>"+ ele.element("資訊內容").getText() +"</text>" +"\r\n";
		        	xml+="\t"+"</article> "+"\r\n";
    			}
    		}
    			xml+="</book>";
    			//指定新檔案生成地址並生成
    			File f = new File("F://new/新的.xml");
    			OutputStream out = null ;
    			out = new FileOutputStream(f);
    			byte b[] =xml.getBytes() ;
    			out.write(b) ;
			} catch (Exception e) {
				e.printStackTrace();
			}
		
		System.out.println("成功了!");
		
	}
	
	public static void main(String[] args) throws DocumentException {
		
		SAXReader red = new SAXReader();
		//讀取檔案
	    Document document = red.read(new File("F://new/舊的.xml"));
		Element node = document.getRootElement();
		Dom4jDemo1 dd = new Dom4jDemo1();
		dd.exportNewXML(node);
		
	}
}

執行結果:

<?xml version="1.0" encoding="UTF-8"?>
<book>
	<edition>1.0</edition>
	<copyright>巴倫創維</copyright>
	<article> 
		<title>資訊標題1</title>
		<crtime>建立日期</crtime>
		<reltime>建立日期</reltime>
		<depiction>內容描述1</depiction>
		<text>資訊內容1</text>
	</article> 
	<article> 
		<title>資訊標題2</title>
		<crtime>建立日期2</crtime>
		<reltime>成文日期2</reltime>
		<depiction>內容描述2</depiction>
		<text>資訊內容2</text>
	</article> 
	<article> 
		<title>資訊標題3</title>
		<crtime>建立日期3</crtime>
		<reltime>成文日期3</reltime>
		<depiction>內容描述3</depiction>
		<text>資訊內容3</text>
	</article> 
	<article> 
		<title>資訊標題4</title>
		<crtime>建立日期4</crtime>
		<reltime>成文日期4</reltime>
		<depiction>內容描述4</depiction>
		<text>資訊內容4</text>
	</article> 
	<article> 
		<title>資訊標題5</title>
		<crtime>成文日期5</crtime>
		<reltime>成文日期5</reltime>
		<depiction>內容描述5</depiction>
		<text>資訊內容5</text>
	</article> 
	<article> 
		<title>資訊標題6</title>
		<crtime>成文日期6</crtime>
		<reltime>成文日期6</reltime>
		<depiction>內容描述6</depiction>
		<text>資訊內容6</text>
	</article> 
</book>
結束。