기상청 날씨 정보가 들어있는 XML 문서를 로딩하고 분석 후 결과 출력하는 예제
- 기상청 RSS
http://www.kma.go.kr/weather/lifenindustry/sevice_rss.jsp
- 기상청 육상 중기예보
http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109
stnId=108(전국)
stnId=109 (서울, 경기)
stnId=105 (강원)
stnId=131 (충청북도)
stnId=133 (충청남도)
stnId=146 (전라북도)
stnId=156 (전라남도)
stnId=143 (경상북도)
stnId=159 (경상남도)
stnId=184 (제주특별자치도)
코드
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%> <%@ page import="java.net.URL"%> <%@ page import="javax.xml.parsers.*"%> <%@ page import="org.w3c.dom.*"%> <%@page import="javax.xml.xpath.*"%> <%@ page import="org.xml.sax.InputSource"%> <% //weather.jsp -> 지역 선택. 서브밋 액션. 날씨 정보 확인 및 출력. //절대경로 확인 String path = request.getContextPath(); String stnId = request.getParameter("stnId"); if (stnId == null) { stnId = "108"; } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = null; String str = String.format("http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=%s", stnId); System.out.println(str); URL url = new URL(str); InputSource is = new InputSource(url.openStream()); doc = builder.parse(is); // XPath에 의한 XML 엘리먼트 탐색 XPath xpath = XPathFactory.newInstance().newXPath(); // 루트 엘리먼트 접근 //Node rootNode = (Node) xpath.compile("/rss").evaluate(doc, XPathConstants.NODE); // System.out.println(rootNode.getNodeName()); //rss //특정 엘리먼트의 텍스트 접근 rss/channel/item/title 뽑아오기 String title = xpath.compile("/rss/channel/item/title").evaluate(doc); // rss/channel/item/description/header/wf String headerWf = xpath.compile("/rss/channel/item/description/header/wf").evaluate(doc); // rss/channel/item/description/body/location -> 복수개 존재하므로 NodeList로 받아온다. NodeList locationList = (NodeList)xpath.compile("/rss/channel/item/description/body/location").evaluate(doc, XPathConstants.NODESET); StringBuilder sb = new StringBuilder(); for (int i=0; i < locationList.getLength(); i++) { Node location = (Node)xpath.compile(String.format("/rss/channel/item/description/body/location[%s]", i+1)).evaluate(doc, XPathConstants.NODE); String city = xpath.compile("city").evaluate(location); sb.append(String.format("<h3>%s</h3>", city)); sb.append(String.format("<table class=\"table\">")); sb.append(String.format("<thead><tr><th>날짜</th><th>날씨</th><th>최저/최고 기온</th><th>신뢰도</th></tr></thead>")); sb.append(String.format("<tbody>")); NodeList dataList = (NodeList)xpath.compile("data").evaluate(location, XPathConstants.NODESET); for (int k=0; k < dataList.getLength(); k++) { //부모 -> 하나밖에 없어야 된다?(->Node) Node dataNode = (Node)xpath.compile(String.format("data[%s]", k+1)).evaluate(location, XPathConstants.NODE); // // -> 자손 탐색 // / -> 루트 String tmEf = xpath.compile(String.format("tmEf", k+1)).evaluate(dataNode); String wf = xpath.compile(String.format("wf", k+1)).evaluate(dataNode); String tmn = xpath.compile(String.format("tmn", k+1)).evaluate(dataNode); String tmx = xpath.compile(String.format("tmx", k+1)).evaluate(dataNode); String reliability = xpath.compile(String.format("reliability", k+1)).evaluate(dataNode); sb.append(String.format("<tr>")); sb.append(String.format("<td>%s</td>", tmEf)); String img = ""; switch(wf){ case "흐림": img = "W_DB04.png"; break; case "비": img = "W_DB05.png"; break; case "비,눈": img = "W_DB06.png"; break; case "눈": img = "W_DB08.png"; break; case "구름조금": img = "W_NB02.png"; break; case "구름많음": img = "W_NB03.png"; break; case "흐리고 비": img = "W_NB08.png"; break; case "구름많고 비": img = "W_NB20.png"; break; } sb.append(String.format("<img src=\""+ path +"/resources/img/%s\">", img)); sb.append(String.format("<td>%s / %s</td>", tmn, tmx)); sb.append(String.format("<td>%s</td>", reliability)); sb.append(String.format("</tr>")); } sb.append(String.format("</tbody>")); } %> <!DOCTYPE html> <html> <head> <title>쌍용교육센터</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Latest compiled JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script> $(document).ready(function() { $("input[value='<%=stnId%>']").attr("checked", "checked"); }); </script> </head> <body> <div class="container"> <h1> 기상청 육상 중기예보 <small>v1.0 by XML</small> </h1> <div class="panel-group"> <div class="panel panel-default"> <div class="panel-heading">지역 선택</div> <div class="panel-body"> <form role="form" method="POST"> <input type="radio" name="stnId" value="108" checked="checked"> 전국 <input type="radio" name="stnId" value="109"> 서울,경기 <input type="radio" name="stnId" value="105"> 강원 <input type="radio" name="stnId" value="131"> 충청북도 <input type="radio" name="stnId" value="133"> 충청남도 <input type="radio" name="stnId" value="146"> 전라북도 <input type="radio" name="stnId" value="156"> 전라남도 <input type="radio" name="stnId" value="143"> 경상북도 <input type="radio" name="stnId" value="159"> 경상남도 <input type="radio" name="stnId" value="184"> 제주특별자치도 <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> <div class="panel panel-default"> <div class="panel-heading">기상 정보 출력</div> <div class="panel-body"> <p> <b><%=title%></b> </p> <p> <%=headerWf%> </p> <%=sb.toString()%> </div> </div> </div> </div> </body> </html> </div> </body> </html> | cs |