하고재비
[JAVA]XML Parsing 본문
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 | import javax.swing.text.html.HTMLEditorKit.Parser; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /* -<item> <dutyAddr>부산광역시 동구 고관로 155, (좌천동)</dutyAddr> <dutyEtc>4째주 일요일 가능 /2007.08./</dutyEtc> <dutyMapimg>봉생병원 후문 앞</dutyMapimg> <dutyName>보생약국</dutyName> <dutyTel1>051-642-0700</dutyTel1> <dutyTime1c>1900</dutyTime1c> <dutyTime1s>0830</dutyTime1s> <hpid>C1200330</hpid> <postCdn1>487</postCdn1> <postCdn2>73 </postCdn2> <rnum>36</rnum> <wgs84Lat>35.13123181989675</wgs84Lat> <wgs84Lon>129.0499687042262</wgs84Lon> </item> */ public class XMLPARSER { private static String getTagValue(String tag, Element eElement) { NodeList nlList = eElement.getElementsByTagName(tag).item(0).getChildNodes(); Node nValue = (Node) nlList.item(0); if (nValue == null) return null; return nValue.getNodeValue(); } public void Paser(String url) { int page = 1; // 페이지 초기값 int count = 0; try { while (true) { // parsing할 url 지정(API 키 포함해서) DocumentBuilderFactory dbFactoty = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactoty.newDocumentBuilder(); Document doc = dBuilder.parse(url+page); // root tag doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); // 파싱할 tag NodeList nList = doc.getElementsByTagName("item"); System.out.println("파싱할 리스트 수 : "+ nList.getLength()); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("######################"); // System.out.println(eElement.getTextContent()); System.out.println("기관명 : " + getTagValue("dutyName", eElement)); System.out.println("주소 : " + getTagValue("dutyAddr", eElement)); System.out.println("전화번호 : " + getTagValue("dutyTel1", eElement)); System.out.println("진료시간 : " + getTagValue("dutyTime1s", eElement) + " - " + getTagValue("dutyTime1c", eElement)); //System.out.println("위도 : " + getTagValue("wgs84Lon", eElement)); //System.out.println("경도 : " + getTagValue("wgs84Lat", eElement)); count++; } // for end } // if end page += 1; System.out.println("page number : " + page); if (page > 12) { break; } } // while end System.out.println("count = "+count); } catch (Exception e) { e.printStackTrace(); } // try~catch end } public static void main(String[] args) { // TODO Auto-generated method stub XMLPARSER xml = new XMLPARSER(); String url = "URL^^"; xml.Paser(url); } } | cs |
참고출처 :: http://blog.naver.com/nonamed0000?Redirect=Log&logNo=220988048654
'JAVA' 카테고리의 다른 글
[JAVA] 상속 (0) | 2017.12.07 |
---|---|
[JAVA] 메소드 Method (0) | 2017.12.07 |
[JAVA] 클래스와 객체 (0) | 2017.12.02 |
[JAVA] 버블소트, 선택정렬 알고리즘 정리 (0) | 2017.11.22 |
[JAVA] String to int & int to String (0) | 2017.11.22 |
Comments