Tạo nút XML DOM
- Trang trước Thay thế nút DOM
- Trang tiếp theo Thêm nút 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);
Giải thích ví dụ:
- Giả sử books.xml đã được tải vào
xmlDoc
trong - Tạo một nút phần tử mới <edition>
- 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); }
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);
Giải thích ví dụ:
- Giả sử tệp books.xml đã được tải vào
xmlDoc
trong - Tạo nút thuộc tính mới
"edition"
- Đặt giá trị của nút thuộc tính này là
"first"
- 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); }
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");
Giải thích ví dụ:
- Giả sử books.xml đã được tải vào
xmlDoc
trong - Đặ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", "第一版"); }
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);
Giải thích ví dụ:
- Giả sử books.xml đã được tải vào
xmlDoc
trong - Tạo phần tử phần tử mới <edition>
- Tạo phần tử văn bản mới chứa văn bản
"first"
- Chèn phần tử văn bản mới này vào phần tử phần tử mới
- 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); }
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);
Giải thích ví dụ:
- Giả sử books.xml đã được tải vào
xmlDoc
trong - Tạo phần tử section CDATA mới
- 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); }
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);
Giải thích ví dụ:
- Giả sử books.xml đã được tải vào
xmlDoc
trong - Tạo phần tử chú thích mới
- 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); }
- Trang trước Thay thế nút DOM
- Trang tiếp theo Thêm nút DOM