참치코더의 꿈 메모장

2021/09/17 공공데이터 포털 데이터 가져오기 - Java 본문

잡다한 웹지식

2021/09/17 공공데이터 포털 데이터 가져오기 - Java

참치깡 2021. 9. 17. 10:04
728x90
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
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
 
public class Project2 {
 
    private static Logger log = Logger.getLogger(Project2.class);   //log4j 연동
    
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
 
        String svcKey = "=서비스키";
        
        
            URL url = new URL("http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaCode"
                    +"?" + URLEncoder.encode("ServiceKey","UTF-8"+ svcKey
                 //+"&" + URLEncoder.encode("ServiceKey","UTF-8") + "=" + URLEncoder.encode(svcKey +"(URL - Encode)", "UTF-8")
                 +"&" + URLEncoder.encode("numOfRows","UTF-8"+ "=" + URLEncoder.encode("10""UTF-8")
                 +"&" + URLEncoder.encode("pageNo","UTF-8"+ "=" + URLEncoder.encode("1""UTF-8")
                 +"&" + URLEncoder.encode("MobileOS","UTF-8"+ "=" + URLEncoder.encode("ETC""UTF-8")
                 +"&" + URLEncoder.encode("MobileApp","UTF-8"+ "=" + URLEncoder.encode("AppTest""UTF-8")
                 +"&" + URLEncoder.encode("arrange","UTF-8"+ "=" + URLEncoder.encode("A""UTF-8")
                 +"&" + URLEncoder.encode("listYN","UTF-8"+ "=" + URLEncoder.encode("Y""UTF-8")
                 +"&" + URLEncoder.encode("areaCode","UTF-8"+ "=" + URLEncoder.encode("2""UTF-8")
                 +"&" + URLEncoder.encode("eventStartDate","UTF-8"+ "=" + URLEncoder.encode("20210101""UTF-8")
                );
            
            //System.out.println(url);
            
            DocumentBuilder builder  = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            
            // DOM 파서로부터 입력받은 문서내용을 파싱하도록 요청
            Document document = builder.parse(url.toString());
            
            // DOM Document 객체로부터 루트 앨리먼트 객체 가져오기
            Element root = document.getDocumentElement();
            
            // item 태그요소 가져와서 NodeList에 
            NodeList itemNodeList = root.getElementsByTagName("item"); 
            
            //String code = root.getElementsByTagName("code").item(0).getTextContent();
           
                for (int i = 0; i < itemNodeList.getLength(); i++) {
                    Element element = (Element) itemNodeList.item(i);
                    
                    String code = root.getElementsByTagName("code").item(i).getTextContent();
                    String name = root.getElementsByTagName("name").item(i).getTextContent();
                    String rnum = root.getElementsByTagName("rnum").item(i).getTextContent();
                    
                    System.out.println("지역코드\t지역명\t일련번호");
                    System.out.println("=======================================");
                    System.out.println(code+"\t"+name+"\t"+rnum );
                    System.out.println("=======================================");
                }
            
    }
 
}
 
cs

 

728x90
Comments