https://intheham.tistory.com/78
[공공데이터] 1. 다운받은 파일 파싱 (csv, json, xml)
공공데이터를 활용하는 방법에는 1. 공공데이터 사이트에서 파일을 다운받아 파싱 2. 온라인에서 바로 파싱 이번에는 1. 다운받은 파일을 파싱하는 법을 알아보자. 0. 파일 넣기 - 우선 다운받은
intheham.tistory.com
commands.properties
/weather/load.do=handler.WeatherRssHandler
/bus/getInfoByRouteID.do=handler.BusHandler
/bus/getListByName.do=handler.BusIdHandler
/bus/stationlist.do=handler.BusStationHandler
1. 기상청 RSS
<a href="${pageContext.request.contextPath}/weather/load.do">기상철 날씨 데이터 확인</a>
public class WeatherVo {
private String city;
private String dateTime;
private String info;
private String tmn; // 최저 온도
private String tmx; // 최고 온도
private String rnst; // 비 올 확률
}
package handler;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import xml.WeatherVo;
public class WeatherRssHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String urlstr = "http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109";
try {
URL url = new URL(urlstr);
URLConnection conn = url.openConnection(); // 네트워크 연결
// // InputStream : 바이트를 읽어옴
// // InputStreamReader : 바이트를 문자로 변환
// // BufferedReader : 속도 업
// BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); // .getInputStream : url에 연결된 자원을 읽을 읽기 스트림을 반환
// StringBuilder sb = new StringBuilder();
// String str = "";
// while((str = br.readLine()) != null) {
// sb.append(str+"\n");
// }
// str = sb.toString();
// System.out.println(str);
// br.close();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder 객체 생성
DocumentBuilder builder = factory.newDocumentBuilder();
// xml 파싱
Document doc = builder.parse(conn.getInputStream());
Element root = doc.getDocumentElement(); // root요소 추출
String author = root.getElementsByTagName("author").item(0).getTextContent();
String category = root.getElementsByTagName("category").item(0).getTextContent();
String title = root.getElementsByTagName("title").item(1).getTextContent(); // 두번째 <title> 태그를 쓰고 싶어서.
// System.out.println("author: " + author);
// System.out.println("category: " + category);
// System.out.println("title: " + title);
ArrayList<WeatherVo>list = new ArrayList<>();
NodeList locs = root.getElementsByTagName("location");
for (int i=0; i<locs.getLength(); i++) {
Element loc = (Element) locs.item(i);
String city = loc.getElementsByTagName("city").item(0).getTextContent();
NodeList datas = loc.getElementsByTagName("data");
for(int j=0; j<datas.getLength(); j++) {
Element data = (Element) datas.item(j);
String dateTime = data.getElementsByTagName("tmEf").item(0).getTextContent();
String info = data.getElementsByTagName("wf").item(0).getTextContent();
String tmn = data.getElementsByTagName("tmn").item(0).getTextContent();
String tmx = data.getElementsByTagName("tmx").item(0).getTextContent();
String rnst = data.getElementsByTagName("rnSt").item(0).getTextContent();
list.add(new WeatherVo(city, dateTime, info, tmn, tmx, rnst));
}
}
request.setAttribute("list", list);
request.setAttribute("author", author);
request.setAttribute("category", category);
request.setAttribute("title", title);
System.out.println(list);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/xml/weatherList.jsp";
}
}
2-1. Bus id 검색
서울특별시_노선정보조회_서비스_활용가이드_20190110.docx
0.34MB
노선 정보 확인
1) api 키 받아서 복사해둔다
- 발급받는 데에 보통 1시간정도 걸리니 미리 받아두기.
2)노선ID로 버스 정보를 획득
url = ' http://ws.bus.go.kr/api/rest/busRouteInfo/getRouteInfo?ServiceKey=복사해둔api키&busRouteId=버스노선ID'
<form action="${pageContext.request.contextPath}/bus/getInfoByRouteID.do" method="POST">
버스노선id: <input type="text" name="busid">
<input type="submit" value="검색">
</form>
package handler;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import xml.WeatherVo;
// 버스 노선 아이디를 입력하면 정보를 주는!
public class BusHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String busid = request.getParameter("busid");
String urlstr = "http://ws.bus.go.kr/api/rest/busRouteInfo/getRouteInfo?ServiceKey=복사해둔API키&busRouteId="
+ busid;
URL url;
try {
url = new URL(urlstr);
URLConnection conn = url.openConnection(); // 네트워크 연결
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder 객체 생성
DocumentBuilder builder = factory.newDocumentBuilder();
// xml 파싱
Document doc = builder.parse(conn.getInputStream());
Element root = doc.getDocumentElement(); // root요소 추출
String headercode = root.getElementsByTagName("headerCd").item(0).getTextContent();
if (!headercode.equals("0")) { // "0"이 아니면 오류. "0"이 정상운행임
String errormsg = root.getElementsByTagName("headerMsg").item(0).getTextContent();
response.setContentType("text/html; charset=UTF-8");
response.getWriter().append(errormsg);
return null;
}
// getElementsByTagName()은 배열을 반환. 불러오는 값이 하나면 무조건 0번방.
// getTextContent() : 태그 사이 값 반환.
String busRouteNm = root.getElementsByTagName("busRouteNm").item(0).getTextContent();
String corpNm = root.getElementsByTagName("corpNm").item(0).getTextContent();
String edStationNm = root.getElementsByTagName("edStationNm").item(0).getTextContent();
String stStationNm = root.getElementsByTagName("stStationNm").item(0).getTextContent();
String firstBusTm = root.getElementsByTagName("firstBusTm").item(0).getTextContent();
String lastBusTm = root.getElementsByTagName("lastBusTm").item(0).getTextContent();
String term = root.getElementsByTagName("term").item(0).getTextContent();
request.setAttribute("busRouteNm", busRouteNm);
request.setAttribute("corpNm", corpNm);
request.setAttribute("edStationNm", edStationNm);
request.setAttribute("stStationNm", stStationNm);
request.setAttribute("firstBusTm", firstBusTm);
request.setAttribute("lastBusTm", lastBusTm);
request.setAttribute("term", term);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/bus/busList.jsp";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>${busRouteNm} 버스 정보</h3>
버스 번호: ${busRouteNm}<br/>
운수 회사: ${corpNm}<br/>
기점: ${stStationNm}<br/>
종점: ${edStationNm}<br/>
첫차 시간: ${firstBusTm}<br/>
막차 시간: ${lastBusTm}<br/>
배차 간격: ${term}<br/>
</body>
</html>
2-2. Bus 번호 검색 (ex. 3을 포함하는 모든 버스)
<form action="${pageContext.request.contextPath}/bus/getListByName.do" method="POST">
버스 번호: <input type="text" name="busnum">
<input type="submit" value="검색">
</form>
public class BusVo {
private String busRouteId; // 버스 아이디
private String busRouteNm; // 버스번호
private String corpNm; // 운수 회사
private String stStationNm; // 기점
private String edStationNm; // 종점
private String firstBusTm; // 첫차시간
private String lastBusTm; // 막차시간
private String term; // 배차간격
}
package handler;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import xml.BusVo;
public class BusIdHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
String busnum = request.getParameter("busnum");
String urlstr = "http://ws.bus.go.kr/api/rest/busRouteInfo/getBusRouteList?ServiceKey=복사해둔API값&strSrch="+ busnum;
URL url;
try {
url = new URL(urlstr);
URLConnection conn = url.openConnection(); // 네트워크 연결
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// DocumentBuilder 객체 생성
DocumentBuilder builder = factory.newDocumentBuilder();
// xml 파싱
Document doc = builder.parse(conn.getInputStream());
Element root = doc.getDocumentElement(); // root요소 추출 (최상단 태그)
String headercode = root.getElementsByTagName("headerCd").item(0).getTextContent();
if (!headercode.equals("0")) {
String errormsg = root.getElementsByTagName("headerMsg").item(0).getTextContent();
response.setContentType("text/html; charset=UTF-8");
response.getWriter().append(errormsg);
return null;
}
ArrayList<BusVo> list = new ArrayList<>();
NodeList buslist = root.getElementsByTagName("itemList"); // <itemList> 태그 사이 값을 다 뽑음
for (int i = 0; i < buslist.getLength(); i++) {
Element bus = (Element) buslist.item(i);
String busRouteId = bus.getElementsByTagName("busRouteId").item(0).getTextContent();
String busRouteNm = bus.getElementsByTagName("busRouteNm").item(0).getTextContent();
String corpNm = bus.getElementsByTagName("corpNm").item(0).getTextContent();
String edStationNm = bus.getElementsByTagName("edStationNm").item(0).getTextContent();
String stStationNm = bus.getElementsByTagName("stStationNm").item(0).getTextContent();
String firstBusTm = bus.getElementsByTagName("firstBusTm").item(0).getTextContent();
String lastBusTm = bus.getElementsByTagName("lastBusTm").item(0).getTextContent();
String term = bus.getElementsByTagName("term").item(0).getTextContent();
list.add(new BusVo(busRouteId, busRouteNm, corpNm, edStationNm, stStationNm, firstBusTm, lastBusTm, term));
}
request.setAttribute("list", list);
System.out.println(list);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/bus/busIdList.jsp";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>검색된 버스 목록</h3>
<table border="1">
<c:forEach var="vo" items="${list}">
<tr><td rowspan="7">${vo.busRouteNm}<br/>
<a href="${pageContext.request.contextPath}/bus/stationlist.do?busid=${vo.busRouteId}">노선확인</a>
<th>노선 id</th><td>${vo.busRouteId}</td></tr>
<tr><th>운수회사</th><td>${vo.corpNm}</td></tr>
<tr><th>기점</th><td>${vo.stStationNm}</td></tr>
<tr><th>종점</th><td>${vo.edStationNm}</td></tr>
<tr><th>첫차시간</th><td>${vo.firstBusTm}</td></tr>
<tr><th>막차시간</th><td>${vo.lastBusTm}</td></tr>
<tr><th>배차간격</th><td>${vo.term}</td></tr>
</c:forEach>
</table>
</body>
</html>
2-3. Bus 노선 확인
public class StationVo {
private String seq; // 순번
private String stationNm; // 정류소 이름
private String direction; // 진행방향
private String gpsX;
private String gpsY;
private String arsId; // 정류소 고유 번호
}
package handler;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import xml.BusVo;
import xml.StationVo;
public class BusStationHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
String busid = request.getParameter("busid");
String urlstr = "http://ws.bus.go.kr/api/rest/busRouteInfo/getStaionByRoute?ServiceKey=복사해둔API값&busRouteId=";
urlstr += busid;
try {
URL url = new URL(urlstr);
URLConnection conn = url.openConnection(); // 네트워크 연결
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); // DocumentBuilder 객체 생성
Document doc = builder.parse(conn.getInputStream()); // xml 파싱
Element root = doc.getDocumentElement(); // root요소 추출 (최상단 태그)
String headercode = root.getElementsByTagName("headerCd").item(0).getTextContent();
if (!headercode.equals("0")) {
String errormsg = root.getElementsByTagName("headerMsg").item(0).getTextContent();
response.setContentType("text/html; charset=UTF-8");
response.getWriter().append(errormsg);
return null;
}
ArrayList<StationVo> list = new ArrayList<>();
NodeList stationlist = root.getElementsByTagName("itemList"); // <itemList> 태그 사이 값을 다 뽑음
String busRouteNm = "";
for (int i = 0; i < stationlist.getLength(); i++) {
Element station = (Element) stationlist.item(i);
if(i==0) { // 가장 처음에만 버스 번호 받고 그 이후엔 받지 말기~
busRouteNm = station.getElementsByTagName("busRouteNm").item(0).getTextContent();
}
String seq = station.getElementsByTagName("seq").item(0).getTextContent();
String stationNm = station.getElementsByTagName("stationNm").item(0).getTextContent();
String direction = station.getElementsByTagName("direction").item(0).getTextContent();
String gpsX = station.getElementsByTagName("gpsX").item(0).getTextContent();
String gpsY = station.getElementsByTagName("gpsY").item(0).getTextContent();
String arsId = station.getElementsByTagName("arsId").item(0).getTextContent();
list.add(new StationVo(seq, stationNm, direction, gpsX, gpsY, arsId));
}
request.setAttribute("busRouteNm", busRouteNm);
request.setAttribute("list", list);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/bus/stationList.jsp";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>${busRouteNm }의 노선 경유 목록</h3>
<table border="1">
<tr><th>순번</th><th>정거장명</th><th>방향</th><th>X좌표</th><th>Y좌표</th><th>고유번호</th></tr>
<c:forEach var="vo" items="${list}">
<tr><td>${vo.seq}</td><td>${vo.stationNm}</td><td>${vo.direction}</td><td>${vo.gpsX}</td><td>${vo.gpsY}</td><td>${vo.arsId}</td></tr>
</c:forEach>
</table>
</body>
</html>