note

xml Url 파싱 본문

JSP/기본

xml Url 파싱

투한 2012. 6. 4. 11:33


package com;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class test {
	public static void main(String[] args){
		try {
			DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
			DocumentBuilder parser = f.newDocumentBuilder();

			Document xmlDoc = null;

			//전국
			//String url ="http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=108";
			String url ="http://www.kma.go.kr/weather/forecast/mid-term-rss.jsp?stnId=108";

			//강원
			//String url ="http://www.kma.go.kr/weather/forecast/mid-term-rss.jsp?stnId=105";

			xmlDoc = parser.parse(url);

			// 루트 엘리먼트 접근
			Element root = xmlDoc.getDocumentElement();

			int length = root.getElementsByTagName("body").getLength();

			for (int i = 0; i < length; i++) {

				Node vNode = root.getElementsByTagName("location").item(i);
				System.out.println("location size : "+root.getElementsByTagName("location").getLength());

				int asd = root.getElementsByTagName("location").getLength();

				for(int z = 0; z < asd; z++){

					vNode = root.getElementsByTagName("location").item(z);

					Node province
					= ((Element) vNode).getElementsByTagName("province").item(i);
					System.out.println("####province : "+province.getTextContent());

					Node city
					= ((Element) vNode).getElementsByTagName("city").item(i);
					System.out.println("####city : "+city.getTextContent());

					vNode = root.getElementsByTagName("data").item(z);

					//int size = 1;
					int size = root.getElementsByTagName("data").getLength();

					//System.out.println("#### SIZE : "+size);

					for (int j = 0; j < size; j++) {
						Node numEf
						= ((Element) vNode).getElementsByTagName("numEf").item(0);
						System.out.println("#### "+numEf.getTextContent()+"일 후 예보");

						System.out.println("####city : "+city.getTextContent());
						System.out.println("####province : "+province.getTextContent());


						vNode = root.getElementsByTagName("data").item(j);


						Node tmEf
						= ((Element) vNode).getElementsByTagName("tmEf").item(0);
						System.out.println("####날짜 : "+tmEf.getTextContent());

						Node wf  
						= ((Element) vNode).getElementsByTagName("wf").item(0);
						System.out.println("####날씨 : "+wf.getTextContent());

						Node tmn
						= ((Element) vNode).getElementsByTagName("tmn").item(0);
						System.out.println("####최저온도 : "+tmn.getTextContent());

						Node tmx
						= ((Element) vNode).getElementsByTagName("tmx").item(0);
						System.out.println("####최고온도 : "+tmx.getTextContent());

						Node reliability
						= ((Element) vNode).getElementsByTagName("reliability").item(0);
						System.out.println("####날씨에 대한 신뢰도 : "+reliability.getTextContent()+"\n\n");
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println(e.toString());
		}
	}
}