XML DOM 노드 생성

새로운 요소 노드를 생성합니다

createElement() 메서드가 새로운 요소 노드를 생성합니다:

예제 1

newElement = xmlDoc.createElement("edition");
xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);

본인이 직접 테스트해 보세요

예제 설명:

  1. books.xml이 이미 로드되어 있는 xmlDoc
  2. 새로운 요소 노드 <edition>를 생성합니다
  3. 이 요소 노드를 첫 번째 <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);

본인이 직접 테스트해 보세요

예제 설명:

  1. 예를 들어, books.xml이 xmlDoc
  2. 새로운 속성 노드를 생성합니다 "edition"
  3. 이 속성 노드의 값을 설정합니다 "first"
  4. 이 새로운 속성 노드를 첫 번째 <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");

본인이 직접 테스트해 보세요

예제 설명:

  1. books.xml이 이미 로드되어 있는 xmlDoc
  2. 첫 번째 <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);

본인이 직접 테스트해 보세요

예제 설명:

  1. books.xml이 이미 로드되어 있는 xmlDoc
  2. 새 요소 노드 <edition>을 생성합니다
  3. 텍스트를 포함하는 새 텍스트 노드를 생성합니다 "first"
  4. 이 새 텍스트 노드를 새 요소 노드에 추가합니다
  5. 새 요소 노드를 첫 번째 <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);

본인이 직접 테스트해 보세요

예제 설명:

  1. books.xml이 이미 로드되어 있는 xmlDoc
  2. 새 CDATA section 노드를 생성합니다
  3. 이 새 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);

본인이 직접 테스트해 보세요

예제 설명:

  1. books.xml이 이미 로드되어 있는 xmlDoc
  2. 새 주석 노드를 생성합니다
  3. 이 새 주석 노드를 첫 번째 <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);
}

본인이 직접 테스트해 보세요