dom4j解析XML简单例子(-)

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Before;
import org.junit.Test;

public class TestDom4j {
	
	private String requestXML = null;
	
	@Before
	public void before() throws IOException{
		/**
		 * 将文件中 转换为 字符串
		 */
		File file = new File("D:\\xmls\\request.xml"); 
		FileReader reader = new FileReader(file);//字符流
		//缓冲字符流
		BufferedReader bufferedReader = new BufferedReader(reader);
		
		String line = "";
		StringBuffer str = new StringBuffer();
		while((line = bufferedReader.readLine()) != null){
			str.append(line);
		}
		requestXML = str.toString();
//	   requestXML = "<?xml version=\"1.0\" encoding=\"GBK\"?>"+
//					"<request version=\"1.0\">"+
//					"<system>"+
//					"<sysid>ESS</sysid>"+
//					"<uid>ESS0025</uid>"+
//					"<servicename>AEGON_WS_ESS_USER_LOGIN</servicename>"+
//					"</system>"+
//					"<queryinfo>"+
//					"<filed name=\"STATUS\">1</filed>"+
//					"<filed name=\"ERRORMASSAGE\"/>"+
//					"<filed name=\"RESULTTYPE\"/>"+
//					"<filed name=\"PATH\"/>"+
//					"<filed name=\"RESULTCOUNT\">0</filed>"+
//					"<filed name=\"TIME\">2014-05-06 14:52:34</filed>"+
//					"</queryinfo>"+
//					"<query>"+
//					"<filed name=\"name\" type=\"String\">李俊华</filed>"+
//					"<filed name=\"sNumber\" type=\"String\">888888888</filed>"+
//					"<filed name=\"password\" type=\"String\">gaoweigang</filed>"+
//					"</query>"+
//					"</request>";
		
	}
	
	
	/**
	 * 将xml封装到Map中
	 * @throws DocumentException 
	 */
	@Test
	public void testParseXmlToMap() throws DocumentException{
		//定义封装XML中query信息的map
		Map<String, String> map = new HashMap<String, String>();
		
		Document document = DocumentHelper.parseText(requestXML);
		//获取root元素
		Element rootElement = document.getRootElement();
		System.out.println(rootElement.getName());//获取根元素的名字:request
		
		//遍历rootElement元素的直接子元素(注意:不会遍历间接子元素)
		for(Iterator i = rootElement.elementIterator(); i.hasNext();){
			//获取根元素直接子元素directChildElement
			Element directChildElement = (Element)i.next();
			//遍历directChildElement元素的直接子元素
			if("query".equals(directChildElement.getName()))
					for(Iterator j = directChildElement.elementIterator(); j.hasNext();){
						Element element = (Element)j.next();
						if("filed".equals(element.getName())){
							
							String key = element.attributeValue("name");
							System.out.println(key);//name, sNumber, password
							
							String value = element.getText();
							System.out.println(value);//李俊华, 88888888, gaoweigang
							
							map.put(key, value);//把数据封装到Map中
						}
						
					}
		}
		
	}
	@Test
	public void testXMLToMap() throws IOException, DocumentException{
		Map<String,String> map = new HashMap<String, String>();
		
		File file = new File("D:\\xmls\\request.xml");
		SAXReader reader = new SAXReader();
		Document document = reader.read(file);
		//获取根元素
		Element  rootElement = document.getRootElement();
		
		//遍历根元素的直接子元素
		for(Iterator i = rootElement.elementIterator(); i.hasNext();){
			Element directChildElement = (Element)i.next();
			if("query".equals(directChildElement.getName())){
				for(Iterator j = directChildElement.elementIterator(); j.hasNext();){
					Element element = (Element)j.next();
					if("filed".equals(element.getName())){
						String key = element.attributeValue("name");
						System.out.println(key);
						String value = element.getText();
						System.out.println(value);
						map.put(key, value);//将数据封装到Map
					}
				}
				
			}
		}
		
	}
}

 

你可能感兴趣的:(dom4j)