반응형
DomThread.java
package xml_pasing;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class DomThread extends Thread {
// 다운로드 받은 문자열을 저장할 변수
String xml;
// 파싱한 결과를 저정할 ArrayList
//첫번째 방법
ArrayList data = new ArrayList();
//두번째 방법
ArrayList> data_1 = new ArrayList>();
// 스레드로 동작할 메서드
public void run() {
// 다운로드 받는 도중 데이터를 저장할 변수
StringBuilder sBuffer = new StringBuilder();
// 다운로드 처리
try {
// 다운로드 받을 변수
String urlAddr = "http://www.kma.go.kr/weather/forecast/mid-term-xml.jsp?stnId=109";
URL url = new URL(urlAddr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn != null) {
// 연결에 대한 설정
conn.setConnectTimeout(10000);
conn.setUseCaches(false);
// 정상적으로 연결에 성명하면
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
// 줄 단위로 읽어오기 위한 BufferedReader 객체 생성
InputStreamReader isr = new InputStreamReader(
conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
while (true) {
String line = br.readLine();
if (line == null)
break;
sBuffer.append(line);
}
br.close();
conn.disconnect();
xml = sBuffer.toString();
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
// xml이 가진 문자열을 파싱
try {
if (xml != null) {
// xml을 파싱해주는 객체를 생성
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
// xml 문자열은 InputStream으로 변환
InputStream is = new ByteArrayInputStream(xml.getBytes());
// 파싱 시작
Document doc = documentBuilder.parse(is);
// 최상위 노드 찾기
Element element = doc.getDocumentElement();
// 원하는 태그 데이터 찾아오기
NodeList items = element.getElementsByTagName("tmx");
NodeList items1 = element.getElementsByTagName("tmn");
NodeList items2 = element.getElementsByTagName("reliability");
// 데이터 개수 찾이오기
int n = items.getLength();
for (int i = 0; i < n; i++) {
HashMap map = new HashMap();
Node item = items.item(i);
Node text = item.getFirstChild();
String itemValue = text.getNodeValue();
map.put("최고온도", itemValue);
Node item1 = items1.item(i);
Node text1 = item1.getFirstChild();
String itemValue1 = text1.getNodeValue();
map.put("최저온도", itemValue1);
Node item2 = items2.item(i);
Node text2 = item2.getFirstChild();
String itemValue2 = text2.getNodeValue();
map.put("신뢰도", itemValue2);
// 찾은 데이터 datadp 추가
data_1.add(map);
data.add(new Data_tansfer(itemValue,itemValue1,itemValue2));
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(data);
System.out.println(data_1);
}
}
Data_tansfer.java
package xml_pasing;
import java.io.*;
public class Data_tansfer implements Serializable{
private String tmx;
private String tmn;
private String reliability;
public Data_tansfer() {
super();
// TODO 자동 생성된 생성자 스텁
}
public String getTmx() {
return tmx;
}
public void setTmx(String tmx) {
this.tmx = tmx;
}
public String getTmn() {
return tmn;
}
public void setTmn(String tmn) {
this.tmn = tmn;
}
public String getReliability() {
return reliability;
}
public void setReliability(String reliability) {
this.reliability = reliability;
}
@Override
public String toString() {
return "Data_tansfer [tmx=" + tmx + ", tmn=" + tmn + ", reliability="
+ reliability + "]";
}
public Data_tansfer(String tmx, String tmn, String reliability) {
this.tmx = tmx;
this.tmn = tmn;
this.reliability = reliability;
}
}
Dom_main.java
package xml_pasing;
public class Dom_main {
public static void main(String[] args) {
// TODO 자동 생성된 메소드 스텁
DomThread th = new DomThread();
th.start();
}
}
'JAVA > Java' 카테고리의 다른 글
| [JAVA] Oracle DB 접속 후 Insert,select 하기 (0) | 2014.11.18 |
|---|---|
| [JAVA] oracle DB 접속하기 (0) | 2014.11.18 |
| [JAVA] 간단한 UDP server, client (0) | 2014.11.18 |
| [JAVA] Thread 를 사용하여 URL 웹 파일 다운로드 (0) | 2014.11.18 |
| [JAVA] UDP 를 사용한 파일 전송 (0) | 2014.11.18 |