XML DOM 파서 오류

XML 파서 오류

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