티스토리 뷰

시행착오를 겪는 중...

 

안되는 코드>>

package com.exam.search;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.springframework.stereotype.Repository;
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;

@Repository
public class SearchkeyDAO {
	
	private String getValue(String tag, Element element) {
		// TODO Auto-generated method stub
		if(element.getElementsByTagName(tag).item(0)== null) {
			return " ";
		}
		NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
		Node node =(Node) nodes.item(0);
		
		return node.getNodeValue();
	}
	
	public List<SearchkeyTO> searchkeyDAO(HttpServletRequest request) {
		
		try {
			//String str = "카라반";
			String str = request.getParameter( "str" );
			System.out.println( "str : " + str );
			
			String keyword = URLEncoder.encode(str, "utf-8");
			System.out.println( "keyword : " + keyword);

			String url = "http://api.visitkorea.or.kr/openapi/service/rest/GoCamping/searchList"
					+ "?ServiceKey=74rd5r1MwtY%2F3dFG0s9I1UDtFBJBj1zjmm0VdZBNsJoslwPnviVh2PeV1vCg%2BtaHuMvN8G1f1PWIwKh3I%2BI0oQ%3D%3D"
					+ "&numOfRows=3107"
					+ "&pageNo=1"
					+ "&MobileOS=ETC"
					+ "&MobileApp=TestApp"
					+ "&keyword=" + keyword;
			System.out.println( "url : " + url );
			
			DocumentBuilderFactory dbFactoty = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactoty.newDocumentBuilder();
			Document doc = dBuilder.parse(url);
				
			doc.getDocumentElement().normalize();
			System.out.println("Root element: " + doc.getDocumentElement().getNodeName()); 
				
			NodeList nodes = doc.getElementsByTagName("item");
			System.out.println("파싱할 리스트 수 : "+ nodes.getLength());
			for( int i=0 ; i<nodes.getLength() ; i++ ) {
				Node node = nodes.item( i );
				
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					Element element = (Element) node;
					System.out.println("캠핑장명:" + getValue("facltNm", element));				
					System.out.println("주소:" + getValue("addr1", element)+" "+ getValue("addr2", element));
					System.out.println("캠핑장 유형 :" + getValue("induty", element));
                }
			}
			
		} 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 null;
	}
	
}

 

 

 

해결했따.!!!!

리나언니랑 힘을 합쳐서 해 했는데 링크문제였던 것 같다..

 

성공 코드>>

package com.exam.search;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.springframework.stereotype.Repository;
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;

@Repository
public class SearchkeyDAO {
		
	public List<SearchkeyTO> searchkeyDAO(String keysearch) {
	      // TODO Auto-generated method stub
	      try {
	         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();      
	         DocumentBuilder builder = factory.newDocumentBuilder();

	         String keyword = URLEncoder.encode( keysearch, "utf-8" );
			System.out.println( "keyword : " + keyword);
				
	         
	         Document document = builder.parse( new URL("https://api.visitkorea.or.kr/openapi/service/rest/GoCamping/searchList?ServiceKey=74rd5r1MwtY%2F3dFG0s9I1UDtFBJBj1zjmm0VdZBNsJoslwPnviVh2PeV1vCg%2BtaHuMvN8G1f1PWIwKh3I%2BI0oQ%3D%3D&numOfRows=10&pageNo=1&MobileOS=ETC&MobileApp=TestApp&keyword=" +keyword).openStream());
	         
	         Element root = document.getDocumentElement();
	         System.out.println(root.toString());	// [response: null] 출력됨
	         
	         NodeList nodes = document.getElementsByTagName("item");
	         
	         for (int i = 0; i < nodes.getLength(); i++) {
	            Node node = nodes.item(i);
	            if (node.getNodeType() == Node.ELEMENT_NODE) {	// node객체가 앨리먼트인것만 화면에 출력
	               Element element = (Element) node;
	               
	               System.out.println("캠핑장명 : " + getValue( "facltNm", element ));	
	               System.out.println("캠핑장 유형 : " + getValue( "induty", element ));
	               System.out.println("캠핑장 주소 : " + getValue( "addr1", element ));
	               
	            }
	         }
	         
	      } catch (MalformedURLException 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 null;
	         
	   }

	   private static String getValue(String tag, Element element) {
	      NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
	      Node node = (Node)nodes.item(0);
	      return node.getNodeValue();
	   }
}

'FINAL PROJECT' 카테고리의 다른 글

[html/css]툴팁 사이즈 조정  (0) 2022.08.11
KEYWORD 검색  (0) 2022.08.10
html button  (0) 2022.08.04
로그인하면 -> 로그아웃 버튼으로 만들기  (0) 2022.08.04
mouseover / mouseout / file download 버튼  (0) 2022.08.02
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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
글 보관함