DOM 트리에는 다양한 유형의 Node 객체가 담겨 있으며, XML 문서 안의 것들은 모두 노드에 해당한다.
XML DOM의 경우 몇 가지 공통적인 DOM 프로퍼티가 있다.
XML DOM의 경우 다음과 같은 몇 가지 공통적인 DOM 메서드가 있다.
XML DOM 메서드 및 프로퍼티 예제는 다음과 같다.
var text="<person>";
text += "<name>name1</name>";
text += "<address>address1</address>";
text += "<phone>phone1</phone>";
text += "</person>";
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(text,"text/xml");
}
else // 인터넷 익스플로러
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(text);
}
var address = xmlDoc.getElementsByTagName("address")[0].childNodes[0].nodeValue;
console.log("Address: " + address);
위 코드를 실행한 결과는 다음과 같다.
Address: address1
노드의 nodeType 프로퍼티는 노드의 타입이며, 다음과 같은 타입이 있다.
| nodeType 명 | nodeType 값 |
|---|---|
| Element | 1 |
| Attribute | 2 |
| Text | 3 |
| Comment | 8 |
| Document | 9 |
XML DOM 노드 타입의 예는 다음과 같다.
var text="<person>";
text += "<name>name1</name>";
text += "<address>address1</address>";
text += "<phone>phone1</phone>";
text += "</person>";
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(text,"text/xml");
}
else // 인터넷 익스플로러
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(text);
}
console.log(xmlDoc.nodeName + ": " +
xmlDoc.nodeType);
console.log(xmlDoc.documentElement.nodeName + ": " +
xmlDoc.documentElement.nodeType);
x=xmlDoc.documentElement.childNodes;
for (i=0;i<x.length;i++)
{
console.log(x[i].nodeName + ": " + x[i].nodeType);
console.log(x[i].childNodes[0].nodeName + ": " + x[i].childNodes[0].nodeType);
}
위 코드를 실행한 결과는 다음과 같다.
#document: 9 person: 1 name: 1 #text: 3 address: 1 #text: 3 phone: 1 #text: 3
| ← 이전 | 홈 | 다음 → |