brograming
공공데이터 활용 url로 정보 가져오기(Bus)_Day43 본문
서울특별시_노선정보조회_서비스_활용가이드_20190110.docx
0.34MB
공공데이터api활용방법_버스.txt
0.00MB
서울특별시_정류소정보조회_서비스_활용가이드_20190524.docx
0.36MB
bus_station_info.xlsx
1.84MB
index
<body>
<h3>공공데이터 활용</h3>
<a href= "${pageContext.request.contextPath }/csv/load.do">csv 데이터 확인</a><br/>
<a href= "${pageContext.request.contextPath }/json/load.do">json 데이터 확인</a><br/>
<a href= "${pageContext.request.contextPath }/xml/load.do">xml 데이터 확인</a><br/>
<a href= "${pageContext.request.contextPath }/weather/load.do">기상청 날씨 데이터 확인</a><br/>
<form action = "${pageContext.request.contextPath }/bus/load.do" method = "post">
버스 정보 확인 : <input type = "text" name = "busid">
<input type = "submit" value = "검색">
</form>
<form action = "${pageContext.request.contextPath }/bus/getlistbyName.do" method = "post">
검색할 버스번호 : <input type = "text" name = "busnm">
<input type = "submit" value = "검색">
</form>
</body>
commands.properties
/csv/load.do=handler.CsvHandler
/json/load.do=handler.JsonHandler
/xml/load.do=handler.XmlHandler
/weather/load.do=handler.WeatherRssHandler
/bus/load.do=handler.BusRouteInfoHandler
/bus/getlistbyName.do=handler.BusListByNameHandler
/bus/stationlist.do=handler.StationListHandler
busroute




▼BusVo
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package xml;
import java.util.Date;
public class BusVo {
private String busRouteId;
private String busRouteNm;
private String corpNm;
private String edStationNm;
private String stStationNm;
private String firstBusTm;
private String lastBusTm;
private String term;
public BusVo() {
}
public BusVo(String busRouteId, String busRouteNm, String corpNm, String edStationNm, String stStationNm,
String firstBusTm, String lastBusTm, String term) {
super();
this.busRouteId = busRouteId;
this.busRouteNm = busRouteNm;
this.corpNm = corpNm;
this.edStationNm = edStationNm;
this.stStationNm = stStationNm;
this.firstBusTm = firstBusTm;
this.lastBusTm = lastBusTm;
this.term = term;
}
public String getBusRouteId() {
return busRouteId;
}
public void setBusRouteId(String busRouteId) {
this.busRouteId = busRouteId;
}
public String getBusRouteNm() {
return busRouteNm;
}
public void setBusRouteNm(String busRouteNm) {
this.busRouteNm = busRouteNm;
}
public String getCorpNm() {
return corpNm;
}
public void setCorpNm(String corpNm) {
this.corpNm = corpNm;
}
public String getEdStationNm() {
return edStationNm;
}
public void setEdStationNm(String edStationNm) {
this.edStationNm = edStationNm;
}
public String getStStationNm() {
return stStationNm;
}
public void setStStationNm(String stStationNm) {
this.stStationNm = stStationNm;
}
public String getFirstBusTm() {
return firstBusTm;
}
public void setFirstBusTm(String firstBusTm) {
this.firstBusTm = firstBusTm;
}
public String getLastBusTm() {
return lastBusTm;
}
public void setLastBusTm(String lastBusTm) {
this.lastBusTm = lastBusTm;
}
public String getTerm() {
return term;
}
public void setTerm(String term) {
this.term = term;
}
@Override
public String toString() {
return "BusVo [busRouteId=" + busRouteId + ", busRouteNm=" + busRouteNm + ", corpNm=" + corpNm
+ ", edStationNm=" + edStationNm + ", stStationNm=" + stStationNm + ", firstBusTm=" + firstBusTm
+ ", lastBusTm=" + lastBusTm + ", term=" + term + "]";
}
}
|
cs |
▼BusRouteInfoHandler.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package handler;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
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.xml.sax.SAXException;
public class BusRouteInfoHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String urlstr = "http://ws.bus.go.kr/api/rest/busRouteInfo/getRouteInfo?ServiceKey=BYgs6%2FjSL0du1z8yK4GxYdW1SepukkJ0gXtUP3tGUQpjThEU4JeQKRlspdSnxTWcjia6U6r5oPxW%2F7tK7HZ2sg%3D%3D&busRouteId=";
String busid = request.getParameter("busid");
urlstr += busid;
try {
URL 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;
}
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/busroute.jsp";
}
}
|
cs |
▼busroute.jsp
<body>
<h3>${busRouteNm } 버스 정보</h3>
버스번호:${busRouteNm }<br/>
운수회사:${corpNm }<br/>
기점역:${stStationNm }<br/>
종점역:${edStationNm }<br/>
첫차시간:${firstBusTm }<br/>
막차시간:${lastBusTm }<br/>
배차간격:${term }<br/>
</body>
buslist

▼BusListByNameHandler.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
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 BusListByNameHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String urlstr = "http://ws.bus.go.kr/api/rest/busRouteInfo/getBusRouteList?ServiceKey=BYgs6%2FjSL0du1z8yK4GxYdW1SepukkJ0gXtUP3tGUQpjThEU4JeQKRlspdSnxTWcjia6U6r5oPxW%2F7tK7HZ2sg%3D%3D&strSrch=";
String busnm = request.getParameter("busnm");
urlstr += busnm;
try {
URL 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");
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);
} 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/getlistbyName.jsp";
}
}
|
cs |
▼buslistbyName.jsp
<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></td>
<th>노선 id</th><td>${vo.busRouteNm }</td></tr>
<tr><th>운수회사</th><td>${vo.busRouteId }</td></tr>
<tr><th>종점</th><td>${vo.corpNm }</td></tr>
<tr><th>기점</th><td>${vo.edStationNm }</td></tr>
<tr><th>첫차시간</th><td>${vo.stStationNm }</td></tr>
<tr><th>막차시간</th><td>${vo.lastBusTm }</td></tr>
<tr><th>배차</th><td>${vo.term }</td></tr>
</c:forEach>

Busstation


▼StationVo
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package xml;
public class StationVo {
private String seq;
private String stationNm;
private String direction;
private String gpsX;
private String gpsY;
private String arsId;
public StationVo() {}
public StationVo(String seq, String stationNm, String direction, String gpsX, String gpsY, String arsId) {
super();
this.seq = seq;
this.stationNm = stationNm;
this.direction = direction;
this.gpsX = gpsX;
this.gpsY = gpsY;
this.arsId = arsId;
}
public String getSeq() {
return seq;
}
public void setSeq(String seq) {
this.seq = seq;
}
public String getStationNm() {
return stationNm;
}
public void setStationNm(String stationNm) {
this.stationNm = stationNm;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public String getGpsX() {
return gpsX;
}
public void setGpsX(String gpsX) {
this.gpsX = gpsX;
}
public String getGpsY() {
return gpsY;
}
public void setGpsY(String gpsY) {
this.gpsY = gpsY;
}
public String getArsId() {
return arsId;
}
public void setArsId(String arsId) {
this.arsId = arsId;
}
@Override
public String toString() {
return "StationVo [seq=" + seq + ", stationNm=" + stationNm + ", direction=" + direction + ", gpsX=" + gpsX
+ ", gpsY=" + gpsY + ", arsId=" + arsId + "]";
}
}
|
cs |
▼StationListHandler.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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.BusListByNameVo;
import xml.StationVo;
public class StationListHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String urlstr = "http://ws.bus.go.kr/api/rest/busRouteInfo/getStaionByRoute?ServiceKey=BYgs6%2FjSL0du1z8yK4GxYdW1SepukkJ0gXtUP3tGUQpjThEU4JeQKRlspdSnxTWcjia6U6r5oPxW%2F7tK7HZ2sg%3D%3D&busRouteId=";
String busid = request.getParameter("busid");
urlstr += busid;
try {
URL url = new URL(urlstr);
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
Element root = doc.getDocumentElement();
String headercode = root.getElementsByTagName("headerCd").item(0).getTextContent();
if(!headercode.equals("0")) {
String errormsg = root.getElementsByTagName("headerMsg").item(0).getTextContent();
response.getWriter().append(errormsg);
System.out.println("여기인가?");
return null;
}
ArrayList<StationVo> list = new ArrayList<>();
NodeList stationlist = root.getElementsByTagName("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 stationRouteNm = 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,stationRouteNm, 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";
}
}
|
cs |
▼stationlist.jsp
<body>
<h3>${busRouteNm }의 경유 정거장 목록</h3>
<table border="1">
<tr><th>순번</th><th>정거장명</th><th>방향</th><th>x좌표</th><th>y좌표</th><th>arsId</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>'Kosta' 카테고리의 다른 글
| [Spring] 05_08 (0) | 2023.05.08 |
|---|---|
| 공공데이터 활용 url로 정보 가져오기(Weather)_Day43 (0) | 2023.04.19 |
| csv, json, xml 공공데이터 활용 및 parsing_Day42 (0) | 2023.04.17 |
| JQUERY_Day40 (0) | 2023.04.13 |
| Properties. load(), keySet() (0) | 2023.04.11 |