brograming
csv, json, xml 공공데이터 활용 및 parsing_Day42 본문
a_utf8.csv
0.00MB
data.xml
0.00MB
serviceAreaFoods.json
0.14MB

1. 각각의 csv, json, xml형태의 공공 데이터를 web-INF파일 아래에 넣어준다.
2. web-INF의 lib에 commands.properties를 작성해준다.
3. 각 데이터의 내용에 맞게 객체를 만들 Vo를 만들어 준다.
4. 핸들러 interface를 만들고 이를 상속받아 csv, json, xml에 맞게 각각의 handler를 만든다.
5. 실행하면 dispatcherServlet을 오가며 데이터를 웹페이지에 보여준다.
CsvHandler
|
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
|
package handler;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import csv.Vo;
//csv파일 파싱해서 웹 페이지에 데이터를 출력
public class CsvHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
//웹에서 사용될 실제 주소로 변경
String path = request.getServletContext().getRealPath("/WEB-INF/files/a_utf8.csv");
try {
//문자 단위로 읽는 스프림 생성
FileReader fr = new FileReader(path);
//파일에서 읽은 데이터 자장할 배열 생성
char [] buf = new char[10000]; // 한줄씩 읽어들이기
//buf크기 만큼 파일에서 읽어서 buf에 저장. 파일 내용이 char 배열 buf에 들어감
fr.read(buf);
String str = new String (buf); //char[]을 String으로 변환
String[] data = str.split("\n"); // str에 들어가 있는 파일내용을 한줄 한줄 쪼갠다. 배열 하나에 한줄
String[] titles = data[0].split(","); //배열[0](0번째줄)에는 제목이 들어가 있는데 ,로 나눈다.
ArrayList<Vo> list = new ArrayList<>();
for(int i=1; i<data.length;i++) { // 내용은 배열[1](1번째줄)에서 마지막까지
String[] vals = data[i].split(",");
if(vals.length<12) { //파일이 마지막중에 enter가 들어가 있어서
continue;
}
list.add(new Vo(vals[0], vals[1], vals[2], vals[3], vals[4],vals[5], vals[6],
vals[7], vals[8], vals[9], vals[10], vals[11])); // csv파일의 한줄이 하나의 객체가 됨
}
request.setAttribute("titles", titles);
request.setAttribute("list", list); //request에 담아 뷰페이지에 보내줌.
fr.close(); //스트림 닫기
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/csv/list.jsp";
}
}
|
cs |
CsvVo
|
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
|
package csv;
public class Vo {
private String arg1;
private String arg2;
private String arg3;
private String arg4;
private String arg5;
private String arg6;
private String arg7;
private String arg8;
private String arg9;
private String arg10;
private String arg11;
private String arg12;
public Vo() {}
public Vo(String arg1, String arg2, String arg3, String arg4, String arg5, String arg6, String arg7, String arg8,
String arg9, String arg10, String arg11, String arg12) {
super();
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
this.arg4 = arg4;
this.arg5 = arg5;
this.arg6 = arg6;
this.arg7 = arg7;
this.arg8 = arg8;
this.arg9 = arg9;
this.arg10 = arg10;
this.arg11 = arg11;
this.arg12 = arg12;
}
public String getArg1() {
return arg1;
}
public void setArg1(String arg1) {
this.arg1 = arg1;
}
public String getArg2() {
return arg2;
}
public void setArg2(String arg2) {
this.arg2 = arg2;
}
public String getArg3() {
return arg3;
}
public void setArg3(String arg3) {
this.arg3 = arg3;
}
public String getArg4() {
return arg4;
}
public void setArg4(String arg4) {
this.arg4 = arg4;
}
public String getArg5() {
return arg5;
}
public void setArg5(String arg5) {
this.arg5 = arg5;
}
public String getArg6() {
return arg6;
}
public void setArg6(String arg6) {
this.arg6 = arg6;
}
public String getArg7() {
return arg7;
}
public void setArg7(String arg7) {
this.arg7 = arg7;
}
public String getArg8() {
return arg8;
}
public void setArg8(String arg8) {
this.arg8 = arg8;
}
public String getArg9() {
return arg9;
}
public void setArg9(String arg9) {
this.arg9 = arg9;
}
public String getArg10() {
return arg10;
}
public void setArg10(String arg10) {
this.arg10 = arg10;
}
public String getArg11() {
return arg11;
}
public void setArg11(String arg11) {
this.arg11 = arg11;
}
public String getArg12() {
return arg12;
}
public void setArg12(String arg12) {
this.arg12 = arg12;
}
@Override
public String toString() {
return "Vo [arg1=" + arg1 + ", arg2=" + arg2 + ", arg3=" + arg3 + ", arg4=" + arg4 + ", arg5=" + arg5
+ ", arg6=" + arg6 + ", arg7=" + arg7 + ", arg8=" + arg8 + ", arg9=" + arg9 + ", arg10=" + arg10
+ ", arg11=" + arg11 + ", arg12=" + arg12 + "]";
}
}
|
cs |
list.jsp
|
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
|
<%@ 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">
<tr>
<c:forEach var = "t" items="${titles }">
<th>${t }</th>
</c:forEach>
</tr>
<c:forEach var = "vo" items="${list }">
<tr>
<td>${vo.arg1 }</td>
<td>${vo.arg2 }</td>
<td>${vo.arg3 }</td>
<td>${vo.arg4 }</td>
<td>${vo.arg5 }</td>
<td>${vo.arg6 }</td>
<td>${vo.arg7 }</td>
<td>${vo.arg8 }</td>
<td>${vo.arg9 }</td>
<td>${vo.arg10 }</td>
<td>${vo.arg11 }</td>
<td>${vo.arg12 }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
|
cs |
JsonHandler
|
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
|
package handler;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import json.FoodVo;
public class JsonHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String path = request.getServletContext().getRealPath("/WEB-INF/files/serviceAreaFoods.json");
try {
FileReader fr = new FileReader(path);
JSONParser parser = new JSONParser();
JSONObject obj = (JSONObject)parser.parse(fr); //json 파싱 / json.parse(txt); areafoods.json파일에 {}로 묶여있어서 객체형태로반환
Iterator iter = obj.keySet().iterator();
ArrayList<FoodVo> list = new ArrayList<>();
while(iter.hasNext()) {
String key = (String) iter.next();
//음식 객체 하나를 꺼냄
JSONObject o = (JSONObject)obj.get(key);
//음식명 추출
JSONArray arr = (JSONArray)o.get( "http://www.w3.org/2000/01/rdf-schema#label");
JSONObject jsonlabel = (JSONObject)arr.get(0);
String label = (String)jsonlabel.get("value");
//가격 추출
JSONArray arr2 = (JSONArray)o.get( "http://data.ex.co.kr:80/link/def/salePrice");
JSONObject jsonprice = (JSONObject)arr2.get(0);
String price = (String)jsonprice.get("value");
//area
JSONArray arr3 = (JSONArray)o.get( "http://data.ex.co.kr:80/link/def/serviceAreaName");
JSONObject jsonarea = (JSONObject)arr3.get(0);
String area = (String)jsonarea.get("value");
//routeName
JSONArray arr4 = (JSONArray)o.get( "http://data.ex.co.kr:80/link/def/routeName");
JSONObject jsonroute = (JSONObject)arr4.get(0);
String routeName = (String)jsonroute.get("value");
//routeName
JSONArray arr5 = (JSONArray)o.get("http://data.ex.co.kr:80/link/def/direction");
JSONObject jsondirection = (JSONObject)arr5.get(0);
String direction = (String)jsondirection.get("value");
list.add(new FoodVo(label, price, area, routeName, direction));
}
request.setAttribute("list", list);
System.out.println(list);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/json/list.jsp";
}
}
|
cs |
JsonVo
|
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
|
package json;
public class FoodVo {
private String label;
private String price;
private String area;
private String routeName;
private String direction;
public FoodVo() {}
public FoodVo(String label, String price, String area, String routeName, String direction) {
super();
this.label = label;
this.price = price;
this.area = area;
this.routeName = routeName;
this.direction = direction;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getRouteName() {
return routeName;
}
public void setRouteName(String routeName) {
this.routeName = routeName;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
@Override
public String toString() {
return "Vo [label=" + label + ", price=" + price + ", area=" + area + ", routeName=" + routeName
+ ", direction=" + direction + "]";
}
}
|
cs |
list.jsp
|
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
|
<%@ 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">
<tr><th>음식명</th><th>가격</th><th>지역명</th><th>고속도로명</th><th>방향</th></tr>
<c:forEach var = "vo" items="${list }">
<tr>
<td>${vo.label }</td>
<td>${vo.price }</td>
<td>${vo.area }</td>
<td>${vo.routeName }</td>
<td>${vo.direction }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
|
cs |
XmlHandler
|
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
|
package handler;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import xml.BookVo;
public class XmlHandler implements Handler {
@Override
public String process(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String path = request.getServletContext().getRealPath("/WEB-INF/files/data.xml");
try {
//xml은 바이트로 되어있어서 fileinputstream으로 읽는다.
FileInputStream fi = new FileInputStream(path);
//DocumentBuilder 객체를 주는 factory객체 생성
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//DocumentBuilder 객체 생성
DocumentBuilder builder = factory.newDocumentBuilder();
//xml 파싱
Document doc = builder.parse(fi);
//1. root 요소를 뽑는다.<books>
Element root = doc.getDocumentElement();
//2. 자식 노드 추출 <book>태그 모두 추출
NodeList list = root.getChildNodes();
ArrayList<BookVo> datas = new ArrayList<>();
for(int i =0; i <list.getLength();i++) {
Node child = list.item(i); //item(i) : nodelist에서 i번째 노드 추출
NodeList infos = child.getChildNodes(); //<book>의 자식 노드들 추출
if(infos.getLength()==0) {
continue;
}
BookVo vo = new BookVo();
for(int j = 0; j<infos.getLength(); j++) {
Node info = infos.item(j);
String tagName = info.getNodeName(); //getNodeName() : 태그명을 추출하는 메서드 (<bookid>, <title> ...)
switch(tagName) {
case "bookid":
vo.setNum(Integer.parseInt(info.getTextContent()));
break;
case "title":
vo.setName(info.getTextContent());
break;
case "author":
vo.setAuthor(info.getTextContent());
break;
case "price":
vo.setPrice(Integer.parseInt(info.getTextContent()));
break;
}
// String val = info.getTextContent(); //getTextContent() : 태그 값 추출
// System.out.println(tagName + ":" + val);
}
System.out.println(vo);
datas.add(vo);
}
request.setAttribute("datas", datas);
} catch (FileNotFoundException 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();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/xml/list.jsp";
}
}
|
cs |
XmlVo
|
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
|
package xml;
public class BookVo {
private int num;
private String name;
private String author;
private int price;
public BookVo(){}
@Override
public String toString() {
return "BookVo [num=" + num + ", name=" + name + ", author=" + author + ", price=" + price + "]";
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public BookVo(int num, String name, String author, int price) {
super();
this.num = num;
this.name = name;
this.author = author;
this.price = price;
}
}
|
cs |
list.jsp
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ 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>xml 데이터 로드</h3>
<table border = "1">
<tr><th>북아이디</th><th>제목</th><th>저자</th><th>가격</th></tr>
<c:forEach var = "vo" items="${datas }">
<tr><td>${vo.num }</td><td>${vo.name }</td><td>${vo.author }</td><td>${vo.price }</td></tr>
</c:forEach>
</table>
</body>
</html>
|
cs |
'Kosta' 카테고리의 다른 글
| 공공데이터 활용 url로 정보 가져오기(Weather)_Day43 (0) | 2023.04.19 |
|---|---|
| 공공데이터 활용 url로 정보 가져오기(Bus)_Day43 (0) | 2023.04.18 |
| JQUERY_Day40 (0) | 2023.04.13 |
| Properties. load(), keySet() (0) | 2023.04.11 |
| CommandHandler_Day38 (0) | 2023.04.11 |