๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŒ  ์ž๋ฐ” ์„œ๋ฒ„ ํŽ˜์ด์ง€ JSP/๐Ÿ›ธ ์ œ์ด์Šจ XML\JSON

4๏ธโƒฃ ์ œ์ด์Šจ JSON - ์ž๊ธฐ๊ฐ€ ์ข‹์•„ํ•˜๋Š”์ฑ… 5๊ฐ€์ง€ ํ…Œ์ด๋ธ” ์‹œ๊ฐํ™” XML+

by Meteora_ 2021. 3. 9.
728x90

์ฝ”๋“œ ํŽผ์น˜๊ธฐ ->

 

 

๋”๋ณด๊ธฐ
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

<style type="text/css">

#id{
	background-color : blue;
}


</style>

</head>
<body>

 <div id="books"></div>
 
 <script type="text/javascript">
 
 let xhttp = new XMLHttpRequest();
 
 xhttp.onreadystatechange = function() {
	if(this.readyState == 4 && this.status == 200){
		xmlFunc(this);
	}
}
 
 xhttp.open('GET','books.xml',true);
 xhttp.send();
 
 function xmlFunc(xml) {

	let xmlDoc =xml.responseXML;

	let rootTagName = xmlDoc.documentElement.nodeName;

	let nodeArr = xmlDoc.documentElement.childNodes;
	
	let nodeName; 
	for (i = 0; i < nodeArr.length; i++) {
		if(nodeArr[i].nodeType == 1){
			nodeName =nodeArr[i].nodeName;
		}
	}

//Table line 
	let out = "<table border= '4'>";
	out += "<col width='80'><col width='160'><col width='150'><col width='200'>";
	out += "<tr>";
	out += "<th>์ฑ… ์ˆœ์„œ</th>";
	
	columnName = xmlDoc.getElementsByTagName( nodeName )[0]; 
	
	let child = columnName.firstChild;

	for (i = 0; i < columnName.childNodes.length; i++) {
		if(child.nodeType == 1){
			out += "<th>" + child.nodeName + "</th>";

		}
		child = child.nextSibling;
	}
	out += "</tr>";
	
//Data line
	
	let len = xmlDoc.getElementsByTagName(nodeName).length; 

	for (var i = 0; i < len; i++) {
		out += "<tr>";
		out += "<th>" + (i + 1) + "</th>";
		
		let dataArr = xmlDoc.getElementsByTagName(nodeName)[i]; //๊ฐ book๋“ค์„ ๊ฐ€์ ธ์˜ค๊ฒŒ๋จ 
		
		child = dataArr.firstChild;
		for ( j = 0; j < dataArr.childNodes.length; j++) {
			if (child.nodeType == 1) {
				out += "<td>" + child.childNodes[0].nodeValue + "</td>";
			}
			child =child.nextSibling;	
		}
		out += "</tr>";
	}
	out += "</table>";
	document.getElementById("books").innerHTML = out;
	
}

 
 </script>

</body>
</html>

 

books.xml

 

์ฝ”๋“œ ํŽผ์น˜๊ธฐ ->

 

๋”๋ณด๊ธฐ
<?xml version="1.0" encoding="UTF-8"?>

<books>
	<book>
		<title>Lord of the ring</title>
		<author>jf.tolkin</author>
		<price>12$</price>
	</book>
	
	<book>
		<title>Harry Porter</title>
		<author>Joanne Rowling</author>
		<price>20$</price>
	</book>
	
	<book>
		<title>The little Prince</title>
		<author>Saint-Exupéry</author>
		<price>9$</price>
	</book>
	
	<book>
		<title>And Then There Were None</title>
		<author>Agatha Christie</author>
		<price>16$</price>
	</book>
	
	<book>
		<title>The Da Vinci Code</title>
		<author>Dan Brown</author>
		<price>30$</price>
	</book>
</books>

 

๋Œ“๊ธ€