Java讀取XML文件

import java.io.File;
import java.sql.SQLException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONObject;
import org.junit.Test;

import com.google.gson.JsonObject;


public class readXML {
	
	@SuppressWarnings("unchecked")
	@Test
	public void read() {
		int i = 0;
		JSONObject obj = new JSONObject();
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(new File("D:/Dashboardsettings.xml"));
			Element root = document.getRootElement();
			System.out.println(root.getName());
			
			List<Element> list = root.elements() ;
			for (Element e:list){
				i++;
				System.out.println(e.elementText("Name"));
				System.out.println(e.elementText("Value"));
				obj.put(e.elementText("Name"), e.elementText("Value"));
			}
			System.out.println(i);
			System.out.println(obj);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章