Gli ultimi tutorial di sviluppo web
 

XML Errori DOM Parser


Errore XML Parser

Quando si tenta di aprire un documento XML, può verificarsi un parser di errori.

Se il parser incontra un errore, è possibile caricare un documento XML contenente la descrizione dell'errore.

L'esempio di codice riportato di seguito tenta di caricare un documento XML che non è ben formato.

Si può leggere di più su XML ben formato in XML Sintassi .

Esempio

<html>
<body>

<p id="demo"></p>

<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (xhttp.readyState == 4 && xhttp.status == 200) {
    myFunction(xhttp);
  }
};
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>
Prova tu stesso "

Guardate il file XML: note_error.xml