๐Ÿ’ป

JavaScript ์˜ ์ดํ•ด - [AJAX] XMLHttpRequest.responseXML ๋ณธ๋ฌธ

KITRI/JAVASCRIPT

JavaScript ์˜ ์ดํ•ด - [AJAX] XMLHttpRequest.responseXML

๋˜ํšจ๋‹ˆ 2020. 7. 16. 16:50

https://developer.mozilla.org/ko/docs/Web/API/XMLHttpRequest/responseXML

 

XMLHttpRequest.responseXML

The XMLHttpRequest.responseXML read-only property returns a Document containing the HTML or XML retrieved by the request; or null if the request was unsuccessful, has not yet been sent, or if the data can't be parsed as XML or HTML.

developer.mozilla.org

 

 

 

responseXML.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="responseXML.js"></script>
</head>
<body>
	<h3>์‘๋‹ต๋‚ด์šฉ</h3>
	
	<form>
		<input id="sndRet" type="button" value="์ •๋ณด์š”์ฒญ" onclick="startRequest()"/>
	</form>
	
	<div id="disp"></div>
</body>
</html>

 

responseXML.js

/**
 * 
 */
/**
 * 
 */
 
 function startRequest(){
    //alert("OK");
    //๊ฐ์ฒด XMLHttpRequest -> ์›น๋ธŒ๋ผ์šฐ์ €
    var xhr = null;
    var arr = new Array();
    
    if(window.XMLHttpRequest){
       xhr = new XMLHttpRequest;
    }else{
       xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    arr.push(xhr);
    alert(arr.join("\n"));
    
    xhr.open("GET","responseXML.xml", true);
    xhr.send();
    xhr.onreadystatechange = resultProcess;
    
 }
 function resultProcess(){
    if(xhr.readyState==4 && xhr.status==200){
        arr.put(xhr.responseXML);
		//alert(ar.join("\n"));
		
		var xmlDoc = xhr.responseXML;
		var studentList = xmlDoc.getElementsByTagName("student");
		var disp = document.getElementById("disp");
		
		for(var i=0; i<studentList.length; i++){
			var student= studentList[i];
			var subElement = student.childNodes;
			
			var div = document.createElement("div");
			
			for(var j=0; j<subElement.length; j++){
				
				var span = document.createElement("span");
				span.innerHTML = subElement[i].childNodes[0].nodeValue +"&nbsp;";
				
				div.appendChild(span);
			}
		}
		disp.appendChild(div);
    }
 }

 

responseXML.xml

<?xml version="1.0" encoding="UTF-8"?>
<member xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			 xsi:schemaLocation="http://www.example.org/responseXML responseXML.xsd"
			 xmlns="http://www.example.org/responseXML">
	<student>
		<stdNumber>1001</stdNumber>
		<stdName>ํ™๊ธธ๋™</stdName>
		<score>95</score>
	</student>

	<student>
		<stdNumber>1002</stdNumber>
		<stdName>๊น€๊ธธ๋™</stdName>
		<score>90</score>
	</student>
	
	<student>
		<stdNumber>1003</stdNumber>
		<stdName>๋ฐ•๊ธธ๋™</stdName>
		<score>80</score>
	</student>
</member>

 

responseXML.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
			targetNamespace="http://www.example.org/responseXML"
			 xmlns="http://www.example.org/responseXML" 
			 elementFormDefault="qualified">
	<xsd:element name="member" type="member"/>
	
	<xsd:complexType name="member">
		<xsd:sequence>
			<xsd:element name="student" minOccurs="1" maxOccurs="unbounded" type="student"/>
		</xsd:sequence>
	</xsd:complexType>
	
	<xsd:complexType name="student">
		<xsd:sequence>
			<xsd:element name="stdNumber" minOccurs="1" maxOccurs="1" type="xsd:string"/>
			<xsd:element name="stdName" minOccurs="1" maxOccurs="1" type="xsd:string"/>
			<xsd:element name="score" minOccurs="1" maxOccurs="1" type="xsd:string"/>
		</xsd:sequence>
	</xsd:complexType>
</xsd:schema>
				
๋ฐ˜์‘ํ˜•
Comments