XML DOM createElementNS() 메서드
정의와 사용법
createElementNS()
메서드는 이름 공간을 가진 요소 노드를 생성
이 메서드는 Element 객체를 반환합니다.
문법
createElementNS(ns,이름)
파라미터 | 설명 |
---|---|
ns | 문자열, 요소 노드의 이름 공간 이름을 정의 |
이름 | 문자열, 요소 노드 이름을 정의 |
예제
아래 코드는 "books.xml"를 xmlDoc에 로드하고, 각 <book> 요소에 이름 공간을 가진 요소 노드를 추가합니다:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); function myFunction(xml) { var x, y, z, i, newel, newtext, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book"); // 이름 공간과 텍스트 노드로 요소 노드를 생성 for (i = 0; i < x.length; i++) { newel = xmlDoc.createElementNS("p", "edition"); newtext = xmlDoc.createTextNode("First"); newel.appendChild(newtext); x[i].appendChild(newel); } // 모든 title과 edition을 출력 y = xmlDoc.getElementsByTagName("title"); z = xmlDoc.getElementsByTagNameNS("p","edition"); for (i = 0; i < y.length; i++) { txt += y[i].childNodes[0].nodeValue + " - " + z[i].childNodes[0].nodeValue + " edition." + "Namespace: " + z[i].namespaceURI + "<br>"; } document.getElementById("demo").innerHTML = txt; }