收藏675
分享
阅读1852
更新时间2025-07-30
当尝试打开 XML 文档时,可能会发生解析器错误。
如果解析器遇到错误,它可能会加载包含错误描述的 XML 文档。
下面的代码示例尝试加载格式不正确的 XML 文档。
您可以在 XML 语法这一章中学习格式良好的 XML。
<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>
参阅 XML 文件:note_error.xml
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.3万人学习
共49课时
77.4万人学习
共29课时
62万人学习
共25课时
39.5万人学习
共43课时
71.3万人学习
共25课时
61.9万人学习
共22课时
23.1万人学习
共28课时
34.1万人学习
共89课时
125.8万人学习