XML DOM 파서 오류
XML 파서 오류
XML 문서를 열려고 하면 파서 오류가 발생할 수 있습니다.
파서에서 오류가 발생하면 오류 설명이 포함된 XML 문서를 로드할 수 있습니다.
아래 코드 예제는 형식이 잘못된 XML 문서를 로드하려고 합니다.
올바른 형식의 XML에 대한 자세한 내용은 XML Syntax 에서 읽을 수 있습니다 .
예시
<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