dom 4 j解析XMLの【添削改査】


一、解析対象XML
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE    SYSTEM "book.dtd">
<  >
	< >
		<  >Java      123</  >
		<  >9.99</  >
		<  >  </  >
		<  >39.00 </  >
	</ >
	< >
		<  >JavaScript    </  >
		<  >  </  >
		<  >11</  >
	</ >
</  ></span>

二、解析プログラム
本人はdom 4 j-1.6.1.jar配合ユニットテストを使用して解析テストを行う
<span style="font-size:14px;">package com.teng.dom4j;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
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 Demo2 {

	//  
	@Test
	public void find() throws Exception{
		
		//1、     
		SAXReader reader = new SAXReader();
		
		//2、    
		Document doc = reader.read("book.xml");
		
		//3、        
		Element root = doc.getRootElement();
		
		//4、           
		String name = root.element(" ").element("  ").getText();
		
		//System.out.println(name);
		
		//5、     < >   <  >  
		List<Element> eleList = root.elements(" ");
		for(Element ele : eleList){
			String names = ele.element("  ").getText();
			System.out.println(names);
		}
	}
	
	//  
	@Test
	public void add() throws Exception{
		//1、     
		SAXReader reader = new SAXReader();
		
		//2、    
		Document doc = reader.read("book.xml");
		
		//3、        
		Element root = doc.getRootElement();
		
		//4、       
		Element newEle = DocumentHelper.createElement("  ");
		newEle.setText("9.99");
		
		//5、    ,         
		root.element(" ").add(newEle);
		
		//6、  XML  
		//   :  Node     write  
		/*Writer writer  = new OutputStreamWriter(new FileOutputStream("book.xml"),"gb2312");
		doc.write(writer);
		writer.close();*/
		
		//   :XMLWriter    
		OutputFormat format = OutputFormat.createCompactFormat();//      
		format.setEncoding("utf-8");
		XMLWriter writer =new XMLWriter(new FileOutputStream("book.xml"),format);
		writer.write(doc);
		writer.close();	
	}
	
	//         
	@Test
	public void add2() throws Exception{
		//1、     
		SAXReader reader = new SAXReader();
		
		//2、    
		Document doc = reader.read("book.xml");
		
		//3、        
		Element root = doc.getRootElement();
		
		//4、       
		Element newEle = DocumentHelper.createElement("  ");
		newEle.setText("9.99");
		
		//     List  ,   2     
		List eles = root.element(" ").elements();//  < >    List  
		eles.add(1, newEle);
		
		//XMLWriter    
		OutputFormat format = OutputFormat.createCompactFormat();//      
		format.setEncoding("utf-8");
		XMLWriter writer =new XMLWriter(new FileOutputStream("book.xml"),format);
		writer.write(doc);
		writer.close();	
	}
	
	//  
	@Test
	public void update() throws Exception{
		//1、     
		SAXReader reader = new SAXReader();
		
		//2、    
		Document doc = reader.read("book.xml");
		
		//3、        
		Element root = doc.getRootElement();
		
		//4、      
		Element priceEle = root.element(" ").element("  ");
		priceEle.setText("7.7 ");
		
		//XMLWriter    
		OutputFormat format = OutputFormat.createCompactFormat();//      
		format.setEncoding("utf-8");
		XMLWriter writer =new XMLWriter(new FileOutputStream("book.xml"),format);
		writer.write(doc);
		writer.close();	
		
	}
	
	//  
	@Test
	public void delete() throws Exception{
		//1、     
		SAXReader reader = new SAXReader();
		
		//2、    
		Document doc = reader.read("book.xml");
		
		//3、        
		Element root = doc.getRootElement();
		
		//        
		Element priceEle = root.element(" ").element("  ");
		
		//         ,    remove()  
		priceEle.getParent().remove(priceEle);
		
		//XMLWriter    
		OutputFormat format = OutputFormat.createCompactFormat();//      
		format.setEncoding("utf-8");
		XMLWriter writer =new XMLWriter(new FileOutputStream("book.xml"),format);
		writer.write(doc);
		writer.close();	
		
	}
	
	
}</span>