๐ป
JavaScript ์ ์ดํด - [AJAX] ๊ธฐ์์ฒญ ๋ฐ์ดํฐ Parsing ๋ณธ๋ฌธ
KITRI/JAVASCRIPT
JavaScript ์ ์ดํด - [AJAX] ๊ธฐ์์ฒญ ๋ฐ์ดํฐ Parsing
๋ํจ๋ 2020. 7. 20. 10:40๋ค์ด๋ก๋ํด์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐํ๊ธฐ
Apache Commons – Apache Commons
Codec, logging, proxy, httpclient๋ฅผ ๋ค์ด๊ฐ Release์ ๋ค์ด๊ฐ์ binary์์ zipํ์ผ๋ก ๋ค์ด๋ฐ์์ค๋๋ค.
โ
1) Codec ๋ค์ด
โ
โ
2) Proxy ๋ค์ด
โ
โ
3) Logging ๋ค์ด
โ
4) httpcomponents ๋ค์ด
โ
http://archive.apache.org/dist/httpcomponents/
Index of /dist/httpcomponents
๋ค์ด๋ก๋๋ฐ์ ํ์ผ์ ์์ถ์ ํ์ด .jarํ์ผ์ WEB-INF/lib ์ ๋ฃ์ด์ค๋๋ค.
๊ธฐ์์ฒญ ๋ฐ์ดํฐ ํ์ฑํ๊ธฐ
http://www.kma.go.kr/search/kmaSearch.jsp
https://www.weather.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109
commandURL.properties
/proxy/parsing.do = com.java.parsing.command.ParsingXMLCommand
/proxy/pXML.do = com.java.parsing.command.ProxyCommand
pXML.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>
<c:set var="root" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${root}/XHR/xhr.js"></script>
<script type="text/javascript">
function toServer(root){
//var url = "https://www.weather.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109";
var url = root+ "/proxy/pXML.do";
sendRequest("GET", url, null, fromServer);
}
function fromServer(){
//alert(xhr.readyState + "," + xhr.status); //4,200
if(xhr.readyState==4 && xhr.status==200){
processXML();
}
}
function processXML(){
var xmlDoc = xhr.responseXML;
console.log(xmlDoc);
var location = xmlDoc.getElementsByTagName("location");
//alert(location.length); //35
var titleWf = xmlDoc.getElementsByTagName("wf");
document.getElementById("titleWf").innerHTML = titleWf[0].childNodes[0].nodeValue; //childNodes[0]๋์ ์ firstChild๋ ๊ฐ๋ฅ
var city = location[1].getElementsByTagName("city");
document.getElementById("city").innerText = city[0].firstChild.nodeValue;
var data = location[1].getElementsByTagName("data");
var wf = data[1].getElementsByTagName("wf");
document.getElementById("wf").innerText = wf[0].firstChild.nodeValue;
}
</script>
</head>
<body>
<input type="button" value="์ค๋์ ๋ ์จ" onclick="toServer('${root}')"/>
<div>
<span id="titleWf" style="color:red"></span><br /><br />
<span id="city" style="color:blue"></span><br /><br />
<span id="wf"></span><br /><br />
</div>
</body>
</html>
ParsionXMLCommand.java
package com.java.parsing.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.java.command.Command;
public class ParsingXMLCommand implements Command {
@Override
public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
return "/WEB-INF/views/ajax/proxy/pXML.jsp";
}
}
ProxyCommand.java
package com.java.parsing.command;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import com.java.command.Command;
public class ProxyCommand implements Command {
@Override
public String proRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
// ํ๋ก์์๋ฒ: ์์คํ
์ ๋ฐฉํ๋ฒฝ์ ๊ฐ์ง๊ณ ์๋ ๊ฒฝ์ฐ, ์ ๊ทผ์ ํ ์ ์๋ค. ์ธ๋ถ์ ํต์ ์ ์ํด ๋ง๋ค์ด ๋์ ์๋ฒ
// ๋ฐฉํ๋ฒฝ ์์ชฝ์ ์๋ ์๋ฒ๋ค์ ์ธ๋ถ ์ฐ๊ฒฐ์ ํ๋ก์ ์๋ฒ๋ฅผ ํตํด ์ด๋ฃจ์ด์ง๋ค.
String url = "https://www.weather.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=109";
GetMethod method = new GetMethod(url);
HttpClient client = new HttpClient();
int statusCode = client.executeMethod(method);
//logger.info(logMsg + statusCode);
if(statusCode==HttpStatus.SC_OK) {
String result = method.getResponseBodyAsString();
//logger.info(logMsg + result);
response.setContentType("application/xml;charset=utf-8"); // application/text, application/json
PrintWriter out = response.getWriter();
out.print(result);
}
return null;
}
}
๋ฐ์ํ
'KITRI > JAVASCRIPT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments