XML DOM getAttributeNS() 메서드

정의와 사용법

getAttributeNS() 이름 공간 URI와 이름을 통해 속성 값을 가져오는 메서드입니다.

문법

elementNode.getAttributeNS(ns,name)
파라미터 설명
ns 필수. 속성 값을 가져올 이름 공간 URI를 지정합니다.
name 필수. 속성 값을 가져올 속성을 지정합니다.

예제

아래 코드는 "books_ns.xml" 파일을 xmlDoc에 로드하고, 첫 번째 <title> 요소에서 "lang" 속성 값을 가져옵니다:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_ns.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var x = xmlDoc.getElementsByTagName("title")[0];
    var ns = "https://www.codew3c.com/meishi/";
    document.getElementById("demo").innerHTML =
    x.getAttributeNS(ns, "lang");
}

본인이 직접 시도해보세요