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

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

3๏ธโƒฃ XML - ๋ฌธ์ž์—ด ๋Œ€์ž…๊ณผ ์‹ค์ œ XMLํ™”

by Meteora_ 2021. 3. 9.
728x90
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<p id="demo">p tag</p>
	
	<script type="text/javascript">
	
	let xmltext = "<bookstore>" +	/*root tag*/
					"<book>" +		/*node*/
						"<title>ํƒˆ๋ฌด๋“œ</title>" +
						"<author>man</author>" +
						"<year>2001</year>" +
					"</book>" +
					
					"<book>" +		/*node*/
						"<title>์ด์†์ด์•ผ๊ธฐ</title>" +
						"<author>man</author>" +
						"<year>2001</year>" +
					"</book>" +
				"</bookstore>";
				
//alert(xmltext);  // XML์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž์—ด				
				
let parser, xmlDoc;

parser = new DOMParser();

xmlDoc = parser.parseFromString(xmltext, "text/html");

alert(xmlDoc);	//์‹ค์ œ XMLํ™”๋œ ๊ฒƒ

document.getElementById("demo").innerHTML
	//= xmlDoc.getElementsByTagName("book")[0].childNodes[0].nodeName;   //->title
	//= xmlDoc.getElementsByTagName("book")[0].childNodes[1].nodeName; 	//->author
	
	//= xmlDoc.getElementsByTagName("book")[0].childNodes[0].childNodes[0].nodeValue; // ํƒˆ๋ฌด๋“œ
	//= xmlDoc.getElementsByTagName("book").length; // ํฐ ๋…ธ๋“œ์ˆ˜ 
	 = xmlDoc.getElementsByTagName("book")[0].childNodes.length;
	</script>
	
</body>
</html>

๋Œ“๊ธ€