๐Ÿ’ป

JavaScript ์˜ ์ดํ•ด - [AJAX] ๊ธฐ์ƒ์ฒญ ๋ฐ์ดํ„ฐ Parsing ๋ณธ๋ฌธ

KITRI/JAVASCRIPT

JavaScript ์˜ ์ดํ•ด - [AJAX] ๊ธฐ์ƒ์ฒญ ๋ฐ์ดํ„ฐ Parsing

๋˜ํšจ๋‹ˆ 2020. 7. 20. 10:40

๋‹ค์šด๋กœ๋“œํ•ด์„œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

http://commons.apache.org/

 

Apache Commons – Apache Commons

Welcome to Apache Commons Apache Commons is an Apache project focused on all aspects of reusable Java components. The Apache Commons project is composed of three parts: The Commons Proper - A repository of reusable Java components. The Commons Sandbox - A

commons.apache.org

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

 

archive.apache.org

Index of /dist/httpcomponents

 

๋‹ค์šด๋กœ๋“œ๋ฐ›์€ ํŒŒ์ผ์„ ์••์ถ•์„ ํ’€์–ด .jarํŒŒ์ผ์„ WEB-INF/lib ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

.jav

 


๊ธฐ์ƒ์ฒญ ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑํ•˜๊ธฐ

 

 

http://www.kma.go.kr/search/kmaSearch.jsp

 

ํ†ตํ•ฉ๊ฒ€์ƒ‰ > ๊ธฐ์ƒ์ฒญ

๋‹จ์–ด์˜ ์ฒ ์ž๊ฐ€ ์ •ํ™•ํ•œ์ง€ ํ™•์ธํ•ด ์ฃผ์„ธ์š” ๊ฒ€์ƒ‰์–ด์˜ ๋‹จ์–ด ์ˆ˜๋ฅผ ์ค„์ด๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ๊ฒ€์ƒ‰์–ด๋กœ ๊ฒ€์ƒ‰ํ•ด ์ฃผ์„ธ์š”. ๋ณด๋‹ค ์ผ๋ฐ˜์ ์ธ ๊ฒ€์ƒ‰์–ด๋กœ ๊ฒ€์ƒ‰ํ•ด ๋ณด์„ธ์š”.

www.kma.go.kr

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;
    }
}
๋ฐ˜์‘ํ˜•
Comments