TestDom讀取XML文件解析DOM

/**
 * 
 */
/**
 * @author 89003422
 *
 */
package com;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


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


public class TestDom{
	
	
	@SuppressWarnings("unchecked")
	public String getXmlString() throws Exception{
		String content ="";
		SAXReader reader = new SAXReader();
		Document doc = reader.read("src/conf/xconf.xml");
		Element root = doc.getRootElement();
		List<Element> passlist = root.elements("storkey");
		
		Map<String, String> map = new HashMap<String, String>();
		for (int i=0;i<passlist.size();i++) {
			Element root2 = passlist.get(i);
			Element desc = root2.element("stor");
			content = desc.getText();
			String [] data = content.split("\\|");
			System.out.println(data[0]+" "+data[1]);
			if(data.length==0){
				return content;
			}else{
				map.put(new String(Base64.decode(data[0])), new String(Base64.decode(data[1])));
			}
		}
		System.out.println(map);
		return content;
	}
	public String getPassString() throws Exception{
		
		//返回讀取指定資源的輸入流  
	    InputStream is= getClass().getResourceAsStream("/conf/xconf.xml");
	    String xmlStr = "";
	    if(is !=null){
	    	String s="";
	    	BufferedReader br=new BufferedReader(new InputStreamReader(is));  
	    while((s=br.readLine())!=null){  
	    	xmlStr+=s;
	    }
	    br.close();
	    }
		
		return xmlStr;
	}
	public static void main(String[] args) throws Exception {
		TestDom res = new TestDom();
		//InputSource xmlStr = res.getResource(); 
		//String xmlStr = res.getResource();
		String xmlStr = res.getPassString();
		System.out.println("xmlStr:"+xmlStr);
		
		Document document = DocumentHelper.parseText(xmlStr);
		
		Element root = document.getRootElement(); //得到根結點,即Response結點
		
		// 拿到根節點的名稱 conf
        System.out.println("根節點:" + root.getName()); 


        // 獲取根節點下的子節點storkey
        Element InterBOSS=root.element("storkey");//獲取子結點
        Iterator<?> iter = InterBOSS.elementIterator("stor"); 
        // 遍歷head節點
        Map<String, String> map = new HashMap<String, String>();
        while (iter.hasNext()) {
        	Element recordEle = (Element) iter.next();
            // 拿到storkey節點下的子節點stor值
           // String title = recordEle.elementTextTrim("stor"); 
            String content = recordEle.getTextTrim(); 
            String [] data = content.split("\\|");
            map.put(data[0], data[1]);
            map.put(data[0], data[1]);
        }
        System.out.println("map:" + map);
		
		//Element orderInfoReq=svcCont.element("OrderInfoReq");//獲取子子子結點
		//String text = document.asXML();
		//System.out.println(text);
		//Element.asXML方法,獲得包括該標籤的所有XML數據
		//boolean res_success = Boolean.parseBoolean(rootElement.elementTextTrim("success"));
        // System.out.println(root.element("body").asXML());
	}
}
發佈了134 篇原創文章 · 獲贊 60 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章