[ajax] 간단한 XML 데이타 번역(파싱)
로빈아빠
본문
간단한 XML 데이타 번역(파싱)
첨부파일에 간단한 예제 첨부
index.html
data.xml
첨부파일에 간단한 예제 첨부
index.html
<HTML> <HEAD> <TITLE>YesYo.com</TITLE> </HEAD> <BODY> <script> function newXMLHttpRequest() { var xmlreq = false; if (window.XMLHttpRequest) { xmlreq = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlreq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e1) { try { xmlreq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { } } } return xmlreq; } function getData() { request = newXMLHttpRequest();//req 객체반환 request.onreadystatechange = function() { if (request.readyState == 4) { // only if "OK" if (request.status == 200) { parseResponse(request); //alert('dddddd'); } else { alert("There was a problem retrieving the XML data:\n" + request.statusText); } } } request.open("GET", "data.xml", true); request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); request.send(null); } function parseResponse(request) { var output = document.getElementById("out"); var outhtml = ""; output.innerHTML = ""; var data = request.responseXML; var Response = data.getElementsByTagName("Response")[0]; var etc = Response.getElementsByTagName("etc")[0]; var memberCnt = parseInt(etc.getAttribute("memberCnt"), 10); outhtml += "memberCnt : "+memberCnt + "<hr />"; var member = etc.getElementsByTagName("member"); var lists = Response.getElementsByTagName("lists")[0]; var itemCnt = Math.ceil(lists.getAttribute("itemCnt")); outhtml += "itemCnt : "+itemCnt + "<hr />"; var item = lists.getElementsByTagName("item"); if(member.length > 0) { for(i=0;i<member.length;i++) { var memberValue = etc.getElementsByTagName("member")[i].firstChild.nodeValue; outhtml += memberValue + "<br />"; } } outhtml += "<hr />"; if(item.length > 0) { for(i=0;i<item.length;i++) { var t1Value = item[i].getElementsByTagName("t1")[0].firstChild.nodeValue; outhtml += t1Value + "<br />"; } } output.innerHTML = outhtml; } </script> <span style="cursor:pointer;" onclick="getData();">Click</span> <div id="out"></div> </BODY> </HTML>
data.xml
<?xml version="1.0" encoding="utf-8"?> <Response> <etc memberCnt='18'> <member>A123</member> <member>B123</member> <member>C123</member> </etc> <lists itemCnt='12.3'> <item> <t1>t11</t1> <t2>t21</t2> <t3>t31</t3> </item> <item> <t1>t12</t1> <t2>t22</t2> <t3>t32</t3> </item> <item> <t1>t13</t1> <t2>t23</t2> <t3>t33</t3> </item> <item> <t1>t14</t1> <t2>t24</t2> <t3>t34</t3> </item> </lists> </Response>
관련링크
댓글목록
등록된 댓글이 없습니다.