-
기상청 XML 파싱javascript/Jquery 2015. 7. 15. 15:181234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /><script src="http://code.jquery.com/jquery-1.11.0.min.js"></script><script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script><script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script><script type="text/javascript" src="./js/jquery.tmpl.min.js"></script><!--크로스 도메인 처리 위해 포함 --><script type="text/javascript" src="js/jquery.xdomainajax.js"></script><title>기상청 정보 파싱</title><script type="text/javascript"><!--$(function(){$.ajax({type:'GET',url: 'http://www.kma.go.kr/XML/weather/sfc_web_map.xml',dataType : 'xml',success: function(result) {callback(result);},error: function(result) {alert("error !" + result);}})});function callback(result){var xml = result.responseText;xmlDoc = $.parseXML( xml ); // XML파싱$xml = $(xmlDoc); // 객체 생성// 날짜 얻기$date = $xml.find("weather"); // weather 태그 찾기var yy = $date.attr("year"); // 속성읽기var mm = $date.attr("month");var dd = $date.attr("day");var hh = $date.attr("hour");$("#lv").append("<li data-role='list-divider' data-theme='c'>"+yy + "년 " + mm + "월 " + dd + "일 " + hh + "시</li>");$("#lv").listview('refresh');var json = [];$local = $xml.find( "local" );$local.each(function(index){var desc = $(this).attr("desc");if(desc=="-") desc="";var desc = desc;var ta = $(this).attr("ta");var data = $(this).text();json.push({"desc":desc,"ta":ta,"data":data});});$("#Template").tmpl(json).appendTo("#lv");$("#lv").listview('refresh');}//--></script><script id="Template" type="text/x-jquery-tmpl"><li> ${data} (현재온도 : ${ta} ${desc})</li></script></head><body><div data-role="page"><div data-role="header"><h1>기상청 정보 - 전국날씨</h1></div><!-- /header --><div role="main" class="ui-content"><ul data-role="listview" id="lv"></ul></div><!-- /content --><div data-role="footer"><h4>Copyright hankyong.com</h4></div><!-- /footer --></div><!-- /page --></body></html>
cs 댓글