Tạo nút XML DOM

Tạo nút phần tử mới

createElement() Phương pháp tạo mới nút phần tử:

Ví dụ 1

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

Thử ngay

Giải thích ví dụ:

  1. Giả sử books.xml đã được tải vào xmlDoc trong
  2. Tạo một nút phần tử mới <edition>
  3. Chèn nút phần tử này vào phần tử đầu tiên <book>

Ví dụ 2

Vòng lặp duyệt qua và thêm một phần tử vào tất cả các phần tử <book>:

for (i = 0; i < xLen; i++) { 
    newEle = xmlDoc.createElement("edition");
    newText = xmlDoc.createTextNode("第一版");
    newEle.appendChild(newText);
    x[i].appendChild(newEle);
}

Thử ngay

Tạo nút thuộc tính mới

createAttribute() Sử dụng để tạo nút thuộc tính mới:

Ví dụ 1

newAtt = xmlDoc.createAttribute("edition");
newAtt.nodeValue = "第一版";
xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);

Thử ngay

Giải thích ví dụ:

  1. Giả sử tệp books.xml đã được tải vào xmlDoc trong
  2. Tạo nút thuộc tính mới "edition"
  3. Đặt giá trị của nút thuộc tính này là "first"
  4. Thêm nút thuộc tính mới này vào phần tử đầu tiên <title>

Ví dụ 2

Vòng lặp duyệt qua tất cả các phần tử <title> và thêm nút thuộc tính mới:

for (i = 0; i < xLen; i++) { 
    newAtt = xmlDoc.createAttribute("edition");
    newAtt.value = "第一版";
    x[i].setAttributeNode(newAtt);
}

Thử ngay

Nếu thuộc tính đã tồn tại, nó sẽ được thay thế bằng thuộc tính mới.

Sử dụng setAttribute() để tạo thuộc tính

Do đó setAttribute() Phương pháp này sẽ tạo thuộc tính mới nếu thuộc tính không tồn tại, vì vậy nó cũng có thể được sử dụng để tạo thuộc tính mới.

Ví dụ 1

xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");

Thử ngay

Giải thích ví dụ:

  1. Giả sử books.xml đã được tải vào xmlDoc trong
  2. Đặt thuộc tính của phần tử đầu tiên <book> là "edition" Giá trị thuộc tính được đặt là "first"

Ví dụ 2

Vòng lặp duyệt qua tất cả các phần tử <title> và thêm thuộc tính mới:

for(i = 0; i < x.length; i++) {
    x[i].setAttribute("edition", "第一版");
}

Thử ngay

Tạo nút văn bản

createTextNode() Phương pháp tạo mới nút văn bản:

Ví dụ 1

newEle = xmlDoc.createElement("edition");
newText = xmlDoc.createTextNode("first");
newEle.appendChild(newText);
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);

Thử ngay

Giải thích ví dụ:

  1. Giả sử books.xml đã được tải vào xmlDoc trong
  2. Tạo phần tử phần tử mới <edition>
  3. Tạo phần tử văn bản mới chứa văn bản "first"
  4. Chèn phần tử văn bản mới này vào phần tử phần tử mới
  5. Chèn phần tử phần tử mới này vào phần tử <book> đầu tiên

Ví dụ 2

Thêm phần tử có phần tử văn bản vào tất cả các phần tử <book>:

for (i = 0; i < xLen; i++) { 
    newEle = xmlDoc.createElement("edition");
    newText = xmlDoc.createTextNode("第一版");
    newEle.appendChild(newText);
    x[i].appendChild(newEle);
}

Thử ngay

Tạo phần tử section CDATA

createCDATASection() Phương thức tạo phần tử section CDATA mới.

Ví dụ 1

newCDATA = xmlDoc.createCDATASection("新年特惠 & 限时折扣");
xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);

Thử ngay

Giải thích ví dụ:

  1. Giả sử books.xml đã được tải vào xmlDoc trong
  2. Tạo phần tử section CDATA mới
  3. Chèn phần tử CDATA mới này vào phần tử <book> đầu tiên

Ví dụ 2

Vòng lặp qua và thêm phần tử CDATA vào tất cả các phần tử <book>:

x = xmlDoc.getElementsByTagName("book");
xLen = x.length;
newtext = "新年特惠 & 限时折扣";
for (i = 0; i < xLen; i++) { 
    newCDATA = xmlDoc.createCDATASection(newtext);
    x[i].appendChild(newCDATA);
}

Thử ngay

Tạo phần tử chú thích

createComment() Phương thức tạo phần tử chú thích mới.

Ví dụ 1

newComment = xmlDoc.createComment("2024 年 2 月修订");
xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);

Thử ngay

Giải thích ví dụ:

  1. Giả sử books.xml đã được tải vào xmlDoc trong
  2. Tạo phần tử chú thích mới
  3. Chèn phần tử chú thích mới này vào phần tử <book> đầu tiên

Ví dụ 2

Vòng lặp qua và thêm phần tử chú thích vào tất cả các phần tử <book>:

x = xmlDoc.getElementsByTagName("book");
xLen = x.length
for (i = 0; i < xLen; i++) { 
    newComment = xmlDoc.createComment("2024 年 2 月修订");
    x[i].appendChild(newComment);
}

Thử ngay