XML DOM 노드 생성
새로운 요소 노드를 생성합니다
createElement()
메서드가 새로운 요소 노드를 생성합니다:
예제 1
newElement = xmlDoc.createElement("edition"); xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
예제 설명:
- books.xml이 이미 로드되어 있는
xmlDoc
중 - 새로운 요소 노드 <edition>를 생성합니다
- 이 요소 노드를 첫 번째 <book> 요소에 추가합니다
예제 2
모든 요소를 순회하며 모든 <book> 요소에 요소를 추가합니다:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("第一版"); newEle.appendChild(newText); x[i].appendChild(newEle); }
새로운 속성 노드를 생성합니다
createAttribute()
새로운 속성 노드를 생성하는 데 사용됩니다:
예제 1
newAtt = xmlDoc.createAttribute("edition"); newAtt.nodeValue = "first edition"; xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);
예제 설명:
- 예를 들어, books.xml이
xmlDoc
중 - 새로운 속성 노드를 생성합니다
"edition"
- 이 속성 노드의 값을 설정합니다
"first"
- 이 새로운 속성 노드를 첫 번째 <title> 요소에 추가합니다
예제 2
모든 <title> 요소를 순회하며 새로운 속성 노드를 추가합니다:
for (i = 0; i < xLen; i++) { newAtt = xmlDoc.createAttribute("edition"); newAtt.value = "first edition"; x[i].setAttributeNode(newAtt); }
이 속성이 이미 존재하면 새로운 속성으로 대체됩니다.
setAttribute()를 사용하여 속성을 생성합니다
다음과 같습니다 setAttribute()
속성이 존재하지 않으면 새로운 속성을 생성하므로, 새로운 속성을 생성하는 데도 사용될 수 있습니다.
예제 1
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
예제 설명:
- books.xml이 이미 로드되어 있는
xmlDoc
중 - 첫 번째 <book> 요소의
"edition"
속성 값으로 설정됩니다"first"
예제 2
모든 <title> 요소를 순회하며 새로운 속성을 추가합니다:
for(i = 0; i < x.length; i++) { x[i].setAttribute("edition", "first edition"); }
텍스트 노드를 생성합니다
createTextNode()
메서드가 새로운 텍스트 노드를 생성합니다:
예제 1
newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("first"); newEle.appendChild(newText); xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
예제 설명:
- books.xml이 이미 로드되어 있는
xmlDoc
중 - 새 요소 노드 <edition>을 생성합니다
- 텍스트를 포함하는 새 텍스트 노드를 생성합니다
"first"
- 이 새 텍스트 노드를 새 요소 노드에 추가합니다
- 새 요소 노드를 첫 번째 <book> 요소에 추가합니다
예제 2
모든 <book> 요소에 텍스트 노드를 포함한 요소 노드를 추가합니다:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("第一版"); newEle.appendChild(newText); x[i].appendChild(newEle); }
CDATA Section 노드를 생성합니다
createCDATASection()
새 CDATA section 노드를 생성하는 메서드
예제 1
newCDATA = xmlDoc.createCDATASection("新年特惠 & 限时折扣"); xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
예제 설명:
- books.xml이 이미 로드되어 있는
xmlDoc
중 - 새 CDATA section 노드를 생성합니다
- 이 새 CDATA 노드를 첫 번째 <book> 요소에 추가합니다
예제 2
순회하여 모든 <book> 요소에 CDATA 부분을 추가합니다:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length; newtext = "新年特惠 & 限时折扣"; for (i = 0; i < xLen; i++) { newCDATA = xmlDoc.createCDATASection(newtext); x[i].appendChild(newCDATA); }
주석 노드를 생성합니다
createComment()
새 주석 노드를 생성하는 메서드
예제 1
newComment = xmlDoc.createComment("2024 년 2 월 수정"); xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);
예제 설명:
- books.xml이 이미 로드되어 있는
xmlDoc
중 - 새 주석 노드를 생성합니다
- 이 새 주석 노드를 첫 번째 <book> 요소에 추가합니다
예제 2
순회하여 모든 <book> 요소에 주석 노드를 추가합니다:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length for (i = 0; i < xLen; i++) { newComment = xmlDoc.createComment("2024 년 2 월 수정"); x[i].appendChild(newComment); }