note

xml Url 파싱 본문

자바/기본

xml Url 파싱

투한 2012. 6. 4. 10:55


String address ="";
		try {

			//116.91722222222222,27.305833333333332
			//江西省抚州市黎川县


			// DOM Document 객체를 생성하는 단계
			DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
			DocumentBuilder parser = f.newDocumentBuilder();

			// XML 파일 파싱하는 단계
			Document xmlDoc = null;
			String url ="http://map.dootms.com:8081/rgeocode/simple?key=c99d0ae1648e5610bd00c5126a4805132ee938ea5c8f5ac0b7ad23c110793ad1fcf4d3c8d41344d5&resType=xml&encode=utf-8&range=3000&roadnum=10&crossnum=3&poinum=10&retvalue=1&sid=7001&region="+CommonUtil.nvl(longs)+","+CommonUtil.nvl(lat)+"&rid=434766";

			//String url ="C:/Users/eric/Desktop/poilist.xml";
			// error position 114.30194444444444,30.711944444444445
			//118.48444444444445위도 : 38.07916666666667
			//String url = "http://map.dootms.com:8081/rgeocode/simple?key=c99d0ae1648e5610bd00c5126a4805132ee938ea5c8f5ac0b7ad23c110793ad1fcf4d3c8d41344d5&resType=xml&encode=utf-8&range=3000&roadnum=10&crossnum=3&poinum=10&retvalue=1&sid=7001&region=&rid=434766";
			//String url = "http://map.dootms.com:8081/rgeocode/simple?key=c99d0ae1648e5610bd00c5126a4805132ee938ea5c8f5ac0b7ad23c110793ad1fcf4d3c8d41344d5&resType=xml&encode=utf-8&range=3000&roadnum=10&crossnum=3&poinum=10&retvalue=1&sid=7001&region="+CommonUtil.nvl(longs)+","+CommonUtil.nvl(lat)+"&rid=434766";

			xmlDoc = parser.parse(url);  // parse는 결과값이라고 생각하자

			// 루트 엘리먼트 접근
			Element root = xmlDoc.getDocumentElement();

			int length = root.getElementsByTagName("spatial").getLength();

			for (int i = 0; i < length; i++) {
				Node vNode = root.getElementsByTagName("province").item(i);

				//성
				Node province
				= ((Element) vNode).getElementsByTagName("name").item(0);

				vNode = root.getElementsByTagName("city").item(i);

				//시
				Node city
				= ((Element) vNode).getElementsByTagName("name").item(0);

				vNode = root.getElementsByTagName("district").item(i);
				//구
				Node district
				= ((Element) vNode).getElementsByTagName("name").item(0);


				//예외처리
				if(!root.getElementsByTagName("roadlist").item(i).getTextContent().equals(""))
					vNode = root.getElementsByTagName("roadlist").item(i);
				else if (!root.getElementsByTagName("poilist").item(i).getTextContent().equals(""))
					vNode = root.getElementsByTagName("poilist").item(i);
				else if(!root.getElementsByTagName("crosslist").item(i).getTextContent().equals(""))
					vNode = root.getElementsByTagName("crosslist").item(i);

				//거리
				//vNode = root.getElementsByTagName("roadlist").item(i);
				//거리2
				//vNode = root.getElementsByTagName("poilist").item(i);
				//거리 3
				//vNode = root.getElementsByTagName("crosslist").item(i);

				//예외처리 2
				if(root.getElementsByTagName("crosslist").item(i).getTextContent().equals("") && root.getElementsByTagName("poilist").item(i).getTextContent().equals("") && root.getElementsByTagName("roadlist").item(i).getTextContent().equals("")){

					address += province.getTextContent(); //성
					address += city.getTextContent();	//시
					address += district.getTextContent(); //구

				}else{

					//거리 이름
					Node roadlist
					= ((Element) vNode).getElementsByTagName("name").item(0);

					//거리의 방위
					Node direction
					= ((Element) vNode).getElementsByTagName("direction").item(0);


					String direction2 =direction.getTextContent();

					direction2 = direction2.replace("East", "东");
					direction2 = direction2.replace("West","西");
					direction2 = direction2.replace("South","南");
					direction2 = direction2.replace("North","北");


					//그 길까지의 거리
					Node distance
					= ((Element) vNode).getElementsByTagName("distance").item(0);

					address += province.getTextContent(); //성
					address += city.getTextContent();	//시
					address += district.getTextContent(); //구
					address += CommonUtil.nvl(roadlist.getTextContent());  //거리 이름
					address += CommonUtil.nvl(direction2);					//거리 방위
					address += CommonUtil.nvl(distance.getTextContent()); 	//떨어진 거리

				}

				System.out.println("###### address :"+address);

			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			System.out.println(e.toString());
		}


'자바 > 기본' 카테고리의 다른 글

자바의 식별자  (0) 2011.12.12