Java XMLファイルの読み込み

1116 ワード

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 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();
		}		
		
	}
}