验证xml

<html><head><meta charset="UTF-8">


<script>
var xt="",h3OK=1
function checkErrorXML(x)
{
xt=""
h3OK=1
checkXML(x)
}

function checkXML(n)
{
var l,i,nam
nam=n.nodeName
if (nam=="h3")
	{
	if (h3OK==0)
		{
		return;
		}
	h3OK=0
	}
if (nam=="#text")
	{
	xt=xt + n.nodeValue + "\n"
	}
l=n.childNodes.length
for (i=0;i<l;i++)
	{
	checkXML(n.childNodes[i])
	}
}

function validateXML()
{
// code for IE
if (window.ActiveXObject)
  {
  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(document.all("validxml").value);

  if(xmlDoc.parseError.errorCode!=0)
    {
    txt="Error Code: " + xmlDoc.parseError.errorCode + "\n";
    txt=txt+"Error Reason: " + xmlDoc.parseError.reason;
    txt=txt+"Error Line: " + xmlDoc.parseError.line;
    alert(txt);
    }
  else
    {
    alert("No errors found");
    }
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
var parser=new DOMParser();
var text=document.getElementById("validxml").value;
var xmlDoc=parser.parseFromString(text,"text/xml");
if (xmlDoc.getElementsByTagName("parsererror").length>0)
    {
    checkErrorXML(xmlDoc.getElementsByTagName("parsererror")[0]);
    alert(xt)
    }
  else
    {
    alert("No errors found");
    }
  }
else
  {
  alert('Your browser cannot handle this script');
  }
}
</script>

<textarea id="validxml" rows="10" cols="75" style="margin: 0px; width: 733px; height: 222px;">
</textarea><br><br>
<input type="button" value="验证XML" onclick="validateXML()">

</body></html>
Last modification:January 1st, 1970 at 08:00 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment