Lỗi phân tích XML DOM
- Trang trước DOM XMLHttpRequest
- Trang tiếp theo Element Schema
Lỗi phân tích XML
Khi cố gắng mở tệp XML, có thể xảy ra lỗi trình phân tích.
Nếu trình phân tích gặp lỗi, nó có thể tải tệp XML chứa mô tả lỗi.
Mã ví dụ sau đây thử tải tệp XML định dạng không đúng.
Bạn có thể học về định dạng XML hợp lệ trong chương XML ngữ pháp.
Mô hình
<html> <body> <p id="demo"></p> <script> var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.this == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "note_error.xml", true); xhttp.send(); function myFunction(xml) { var parser, xmlDoc; parser = new DOMParser(); xmlDoc = parser.parseFromString(xml.responseText,"text/xml"); document.getElementById("demo").innerHTML = myLoop(xmlDoc.documentElement); } function myLoop(x) { var i, y, xLen, txt; txt = ""; x = x.childNodes; xLen = x.length; for (i = 0; i < xLen ;i++) { y = x[i]; if (y.nodeType != 3) { if (y.childNodes[0] != undefined) { txt += myLoop(y); } } else { txt += y.nodeValue + "<br>"; } } return txt; } </script> </body> </html>
Xem tệp XML: note_error.xml
- Trang trước DOM XMLHttpRequest
- Trang tiếp theo Element Schema