https://www.data.go.kr/
XML로 데이터 받아오기
<?xml version="1.0" encoding="UTF-8"?>
-<response>
-<header>
<resultCode>00</resultCode>
<resultMsg>NORMAL SERVICE.</resultMsg>
</header>
-<body>
-<items>
-<item>
<dutyAddr>대전광역시 동구 계족로 362, 성남약국 1층 (성남동)</dutyAddr>
<dutyName>성남약국</dutyName>
<dutyTel1>042-672-2957</dutyTel1>
<dutyTime1c>1900</dutyTime1c>
<dutyTime1s>0900</dutyTime1s>
<dutyTime2c>1900</dutyTime2c>
<dutyTime2s>0900</dutyTime2s>
<dutyTime3c>1900</dutyTime3c>
<dutyTime3s>0900</dutyTime3s>
<dutyTime4c>1900</dutyTime4c>
<dutyTime4s>0900</dutyTime4s>
<dutyTime5c>1900</dutyTime5c>
<dutyTime5s>0900</dutyTime5s>
<dutyTime6c>1300</dutyTime6c>
<dutyTime6s>0900</dutyTime6s>
<hpid>C1601311</hpid>
<postCdn1>345</postCdn1>
<postCdn2>90 </postCdn2>
<rnum>1</rnum>
<wgs84Lat>36.3444243564852</wgs84Lat>
<wgs84Lon>127.434066050389</wgs84Lon>
</item>
-<item>
<dutyAddr>경기도 화성시 동탄신리천로1길 73, 103호 (산척동)</dutyAddr>
<dutyName>호반약국</dutyName>
<dutyTel1>031-374-1176</dutyTel1>
<dutyTime1c>1800</dutyTime1c>
<dutyTime1s>0900</dutyTime1s>
<dutyTime2c>1800</dutyTime2c>
<dutyTime2s>0900</dutyTime2s>
<dutyTime3c>1800</dutyTime3c>
<dutyTime3s>0900</dutyTime3s>
<dutyTime4c>1800</dutyTime4c>
<dutyTime4s>0900</dutyTime4s>
<dutyTime5c>1800</dutyTime5c>
<dutyTime5s>0900</dutyTime5s>
<hpid>C2108752</hpid>
<postCdn1>184</postCdn1>
<postCdn2>89 </postCdn2>
<rnum>2</rnum>
<wgs84Lat>37.179441394088</wgs84Lat>
<wgs84Lon>127.119338868475</wgs84Lon>
</item>
-<item>
<dutyAddr>경기도 안산시 상록구 광덕1로 355, 데코스포텔 101,102호 (이동)</dutyAddr>
<dutyName>보세온누리약국</dutyName>
<dutyTel1>031-415-7755</dutyTel1>
<dutyTime1c>2100</dutyTime1c>
<dutyTime1s>0900</dutyTime1s>
<dutyTime2c>2100</dutyTime2c>
<dutyTime2s>0900</dutyTime2s>
<dutyTime3c>2100</dutyTime3c>
<dutyTime3s>0900</dutyTime3s>
<dutyTime4c>2100</dutyTime4c>
<dutyTime4s>0900</dutyTime4s>
<dutyTime5c>2100</dutyTime5c>
<dutyTime5s>0900</dutyTime5s>
<dutyTime6c>1800</dutyTime6c>
<dutyTime6s>0900</dutyTime6s>
<hpid>C2104025</hpid>
<postCdn1>154</postCdn1>
<postCdn2>94 </postCdn2>
<rnum>3</rnum>
<wgs84Lat>37.307074008954494</wgs84Lat>
<wgs84Lon>126.8487464523864</wgs84Lon>
</item>
-<item>
<dutyAddr>서울특별시 강남구 선릉로 431, 에스케이허브오피스텔 1층 (역삼동)</dutyAddr>
<dutyName>타임약국</dutyName>
<dutyTel1>02-539-0201</dutyTel1>
<dutyTime1c>2030</dutyTime1c>
<dutyTime1s>0930</dutyTime1s>
<dutyTime2c>2030</dutyTime2c>
<dutyTime2s>0930</dutyTime2s>
<dutyTime3c>2030</dutyTime3c>
<dutyTime3s>0930</dutyTime3s>
<dutyTime4c>2030</dutyTime4c>
<dutyTime4s>0930</dutyTime4s>
<dutyTime5c>2030</dutyTime5c>
<dutyTime5s>0930</dutyTime5s>
<dutyTime6c>1630</dutyTime6c>
<dutyTime6s>0930</dutyTime6s>
<hpid>C1108221</hpid>
<postCdn1>062</postCdn1>
<postCdn2>12 </postCdn2>
<rnum>4</rnum>
<wgs84Lat>37.503434127300004</wgs84Lat>
<wgs84Lon>127.049105761</wgs84Lon>
</item>
-<item>
<dutyAddr>서울특별시 강남구 삼성로 707, 1층(청담동,청담빌딩)</dutyAddr>
<dutyMapimg>청담빌딩 1층</dutyMapimg>
<dutyName>타조약국</dutyName>
<dutyTel1>02-3445-0997</dutyTel1>
<dutyTime1c>1900</dutyTime1c>
<dutyTime1s>0900</dutyTime1s>
<dutyTime2c>1900</dutyTime2c>
<dutyTime2s>0900</dutyTime2s>
<dutyTime3c>1900</dutyTime3c>
<dutyTime3s>0900</dutyTime3s>
<dutyTime4c>1900</dutyTime4c>
<dutyTime4s>0900</dutyTime4s>
<dutyTime5c>1900</dutyTime5c>
<dutyTime5s>0900</dutyTime5s>
<dutyTime6c>1700</dutyTime6c>
<dutyTime6s>0900</dutyTime6s>
<hpid>C1103798</hpid>
<postCdn1>060</postCdn1>
<postCdn2>68 </postCdn2>
<rnum>5</rnum>
<wgs84Lat>37.5192663683</wgs84Lat>
<wgs84Lon>127.0496002823</wgs84Lon>
</item>
-<item>
<dutyAddr>서울특별시 강남구 선릉로86길 17 (대치동)</dutyAddr>
<dutyName>테헤란건강약국</dutyName>
<dutyTel1>02-556-4166</dutyTel1>
<dutyTime1c>2200</dutyTime1c>
<dutyTime1s>0900</dutyTime1s>
<dutyTime2c>2200</dutyTime2c>
<dutyTime2s>0900</dutyTime2s>
<dutyTime3c>2200</dutyTime3c>
<dutyTime3s>0900</dutyTime3s>
<dutyTime4c>2200</dutyTime4c>
<dutyTime4s>0900</dutyTime4s>
<dutyTime5c>2200</dutyTime5c>
<dutyTime5s>0900</dutyTime5s>
<dutyTime6c>2000</dutyTime6c>
<dutyTime6s>0900</dutyTime6s>
<dutyTime7c>1930</dutyTime7c>
<dutyTime7s>1530</dutyTime7s>
<dutyTime8c>1930</dutyTime8c>
<dutyTime8s>1530</dutyTime8s>
<hpid>C1100829</hpid>
<postCdn1>061</postCdn1>
<postCdn2>92 </postCdn2>
<rnum>6</rnum>
<wgs84Lat>37.5035666455</wgs84Lat>
<wgs84Lon>127.051064653</wgs84Lon>
</item>
-<item>
<dutyAddr>서울특별시 강남구 언주로147길 6 (논현동)</dutyAddr>
<dutyMapimg>1층</dutyMapimg>
<dutyName>트리스약국</dutyName>
<dutyTel1>02-541-4545</dutyTel1>
<dutyTime1c>1300</dutyTime1c>
<dutyTime1s>0900</dutyTime1s>
<dutyTime2c>1300</dutyTime2c>
<dutyTime2s>0900</dutyTime2s>
<dutyTime3c>1300</dutyTime3c>
<dutyTime3s>0900</dutyTime3s>
<dutyTime4c>1300</dutyTime4c>
<dutyTime4s>0900</dutyTime4s>
<dutyTime5c>1300</dutyTime5c>
<dutyTime5s>0900</dutyTime5s>
<dutyTime6c>1600</dutyTime6c>
<dutyTime6s>0900</dutyTime6s>
<hpid>C1103453</hpid>
<postCdn1>060</postCdn1>
<postCdn2>48 </postCdn2>
<rnum>7</rnum>
<wgs84Lat>37.52009369371839</wgs84Lat>
<wgs84Lon>127.0336871178174</wgs84Lon>
</item>
-<item>
<dutyAddr>서울특별시 강남구 도산대로 108 (논현동) 1층 일부</dutyAddr>
<dutyName>트리플약국</dutyName>
<dutyTel1>02-517-5256</dutyTel1>
<dutyTime1c>2130</dutyTime1c>
<dutyTime1s>1000</dutyTime1s>
<dutyTime2c>2130</dutyTime2c>
<dutyTime2s>1000</dutyTime2s>
<dutyTime3c>2130</dutyTime3c>
<dutyTime3s>1000</dutyTime3s>
<dutyTime4c>2130</dutyTime4c>
<dutyTime4s>1000</dutyTime4s>
<dutyTime5c>2130</dutyTime5c>
<dutyTime5s>1000</dutyTime5s>
<dutyTime6c>1900</dutyTime6c>
<dutyTime6s>1000</dutyTime6s>
<hpid>C1107165</hpid>
<postCdn1>060</postCdn1>
<postCdn2>38 </postCdn2>
<rnum>8</rnum>
<wgs84Lat>37.51619354533293</wgs84Lat>
<wgs84Lon>127.02065462602883</wgs84Lon>
</item>
-<item>
<dutyAddr>서울특별시 강남구 광평로 196, 202호 (수서동, 제이스프라자)</dutyAddr>
<dutyName>튼튼약국</dutyName>
<dutyTel1>02-451-9111</dutyTel1>
<dutyTime1c>1330</dutyTime1c>
<dutyTime1s>0900</dutyTime1s>
<dutyTime2c>1330</dutyTime2c>
<dutyTime2s>0900</dutyTime2s>
<dutyTime3c>1330</dutyTime3c>
<dutyTime3s>0900</dutyTime3s>
<dutyTime4c>1330</dutyTime4c>
<dutyTime4s>0900</dutyTime4s>
<dutyTime5c>1330</dutyTime5c>
<dutyTime5s>0900</dutyTime5s>
<hpid>C1107712</hpid>
<postCdn1>063</postCdn1>
<postCdn2>62 </postCdn2>
<rnum>9</rnum>
<wgs84Lat>37.4840026375</wgs84Lat>
<wgs84Lon>127.094269484</wgs84Lon>
</item>
-<item>
<dutyAddr>서울특별시 강남구 개포로 615, 석탑프라자 305-1호 (개포동)</dutyAddr>
<dutyMapimg>수서경찰서 인근</dutyMapimg>
<dutyName>튼튼중앙약국</dutyName>
<dutyTel1>02-451-0530</dutyTel1>
<dutyTime1c>1900</dutyTime1c>
<dutyTime1s>0930</dutyTime1s>
<dutyTime2c>1900</dutyTime2c>
<dutyTime2s>0930</dutyTime2s>
<dutyTime3c>1900</dutyTime3c>
<dutyTime3s>0930</dutyTime3s>
<dutyTime4c>1900</dutyTime4c>
<dutyTime4s>0930</dutyTime4s>
<dutyTime5c>1900</dutyTime5c>
<dutyTime5s>0930</dutyTime5s>
<dutyTime6c>1500</dutyTime6c>
<dutyTime6s>0930</dutyTime6s>
<hpid>C1107702</hpid>
<postCdn1>063</postCdn1>
<postCdn2>35 </postCdn2>
<rnum>10</rnum>
<wgs84Lat>37.4926585332</wgs84Lat>
<wgs84Lon>127.0755862212</wgs84Lon>
</item>
</items>
<numOfRows>10</numOfRows>
<pageNo>1</pageNo>
<totalCount>23747</totalCount>
</body>
</response>
이 뒤에 값이 실제 키 값
라이브러리 추가
이거 해서 오류남 아래처럼해라
3개 생성 파일
@PharmacyController
index.html 테스트
만약 되어야하는데 잘 안될 때는?
단점 2개 생김
생성
package com.bitc.xmltest2.dto;
public class PharmacyFullDataItemDto {
private String dutyAddr;
private String dutyName;
private String dutyTel1;
private String dutyTime1c;
private String dutyTime1s;
private String dutyTime2c;
private String dutyTime2s;
private String dutyTime3c;
private String dutyTime3s;
private String dutyTime4c;
private String dutyTime4s;
private String dutyTime5c;
private String dutyTime5s;
private String dutyTime6s;
private String dutyTime6c;
private String rnum;
private String hpid;
private String postCdn1;
private String postCdn2;
private String wgs84Lat;
private String wgs84Lon;
}
dto 생성 - 5개
package com.bitc.xmltest2.dto;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "response")
//@XmlRootElement 해당 클래스의 부모 태그. response를 부모태그로 인식하겠다(연결)
public class PharmacyFullDataDto {
private PharmacyFullDataHeaderDto header; //dto 2개 생성
private PharmacyFullDataBodyDto body;
@XmlElement(name = "header")
// header와 연결하겠다
public PharmacyFullDataHeaderDto getHeader() {
return header;
}
public void setHeader(PharmacyFullDataHeaderDto header) {
this.header = header;
}
@XmlElement(name = "body")
public PharmacyFullDataBodyDto getBody() {
return body;
}
public void setBody(PharmacyFullDataBodyDto body) {
this.body = body;
}
}
package com.bitc.xmltest2.dto;
public class PharmacyFullDataHeaderDto {
private String resultCode;
private String resultMsg;
}
package com.bitc.xmltest2.dto;
public class PharmacyFullDataBodyDto {
private int numOfRows;
private int pageNo;
private int totalCount;
// 그 자체로 데이터 나오는 애들은 그냥 쓰면 되고 아닌 애들은 클래스만들어야함
private PharmacyFullDataItemsDto items;
}
package com.bitc.xmltest2.dto;
import java.util.List;
public class PharmacyFullDataItemsDto {
private List<PharmacyFullDataItemDto> itemList;
}
package com.bitc.xmltest2.dto;
public class PharmacyFullDataItemDto {
private String dutyAddr;
private String dutyName;
private String dutyTel1;
private String dutyTime1c;
private String dutyTime1s;
private String dutyTime2c;
private String dutyTime2s;
private String dutyTime3c;
private String dutyTime3s;
private String dutyTime4c;
private String dutyTime4s;
private String dutyTime5c;
private String dutyTime5s;
private String dutyTime6s;
private String dutyTime6c;
private String rnum;
private String hpid;
private String postCdn1;
private String postCdn2;
private String wgs84Lat;
private String wgs84Lon;
}
PharmacyFullDataDto에 getter,setter생성
받은 파일 경로 위치 저장해짐
PharmacyFullDataServiceImpl
package com.bitc.xmltest2.service;
import com.bitc.xmltest2.dto.*;
import org.springframework.stereotype.Service;
import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.List;
@Service
public class PharmacyFullDataServiceImpl implements PharmacyFullDataService{
// 해당 인터페이스에 모든 메서드 구현을 하지 않아서 에러남.
@Override
public List<PharmacyFullDataItemDto> getItemList() throws Exception{
JAXBContext jc = JAXBContext.newInstance(PharmacyFullDataDto.class);
Unmarshaller um = jc.createUnmarshaller();
PharmacyFullDataDto fullData = (PharmacyFullDataDto) um.unmarshal(new File("C://java505//pharmacy.xml"));
PharmacyFullDataHeaderDto header = fullData.getHeader();
PharmacyFullDataBodyDto body = fullData.getBody();
PharmacyFullDataItemsDto items = body.getItems();
List<PharmacyFullDataItemDto> itemList = items.getItemList();
return itemList;
}
}
/pharmacy/fullData로 들어감.
테스트하면 이렇게 나옴
pharmacyFullData.html
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<p th:text="${pharmacyDatas}"></p>
<!--controller에서 가져온거임-->
</body>
</html>
pharmacyFullData.html 화면 수정
<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<!--<p th:text="${pharmacyDatas}"></p>-->
<!--controller에서 가져온거임-->
<div class="container mt-5 p-5">
<div class="">
<h1 class="text-center">전국 약국 정보 xml 파일 파싱</h1>
</div>
<div class="row">
<div class="">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>상호명</th>
<th>주소</th>
<th>전화번호</th>
<th>시작시간</th>
<th>종료시간</th>
</tr>
</thead>
<tbody>
<tr th:if="${#lists.size(pharmacyDatas)}>0" th:each="item : ${pharmacyDatas}">
<td th:text="${item.dutyName}"></td>
<td th:text="${item.dutyAddr}"></td>
<td th:text="${item.dutyTel1}"></td>
<td th:text="${item.dutyTime1s}"></td>
<td th:text="${item.dutyTime1c}"></td>
</tr>
<tr th:unless="${#lists.size(pharmacyDatas)}>0">
<td colspan="5">조회된 내용이 없습니다</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
이름바꿔줌
@RequestMapping(value = "/pharmacy/fullDataUrl", method = RequestMethod.POST)
public Object getFullDataAjax() throws Exception {
return "";
}
ajax로 가져올 거임
// https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.3'
// https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl
implementation 'com.sun.xml.bind:jaxb-impl:2.3.3'
s빼야 한다.
build_gradle 은 이렇게 수정해야 한다!!
버튼 누르면 이렇게 된다.
application properties에 추가
컨트롤러에 추가
html에 추가