Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Syntax validator that works with Mozilla. 1

Status
Not open for further replies.

celia05es

Programmer
Jan 10, 2002
81
0
0
US
Hello,
I have the following javascription function that validates a xml document that is contained within a textarea called "validxml".
Code:
function validateXML()
{
  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;
    document.forms["rule"].validxml.focus();
    alert(txt);
  }
  else
  {
    alert("No errors found");
  }
}
The problem is that this code only works for IE.... I need it to work with Netscape/Mozilla
Could you help me please?

THank you
 
The script you are writing is only checking for the ActiveXObject, which is only understood by IE.

You need the following code:

Code:
if (document.implementation && document.implementation.createDocument) { //Mozilla browsers
      xmlDoc = document.implementation.createDocument("", "", null);
      xmlDoc.async = isAsync;
   }
   else if (window.ActiveXObject) //IE browser
   {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = isAsync;
   }



<.

 
I have tried what you mentioned but it is not working.
It works fine for Explorer but for Mozilla... the problem lies with the following line:
Code:
xmlDoc.async = isAsync;
I put an alarm before and after the line, and only the alarm before the line is fired.

Could you help please?
 
I am rather new in this and though the link seems interesting I could not see how to make it work with Mozilla.
I have tried for Mozilla:
Code:
var sXml = document.forms[0].validxml.value;
var oParser = new DOMParser();
var oXmlDom = oParser.parseFromString(sXml, "text/xml");
And it does not work because of the last line (oParser).

I don't know what to do!

Would it be too much to ask to tell me what I should do in order to check the XML syntax of a document (contained into a textaread call validxml) with Mozilla?
 
>And it does not work because of the last line (oParser).
In what sense it does not work?
 
If I put an alarm before and after the line, I can see that the command is not executed ... as if it was not recognized!
Please, help me!
 
[1] If you put the alert after the conditional on parseError.errorCode lines, it sure won't work because .parseError is proprietary to msxml. For moz parser, it will return the error within the xmlDoc itself.

[2] You can discover the whole error message like this.
[tt]
function validateXML() {
var xmlDoc;
if (document.implementation && document.implementation.createDocument) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(document.rule.validxml.value, "text/xml");
} else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.loadXML (document.rule.validxml.value);
} else {
alert('Your browser can\'t handle this script');
return;
}

//error handling
var txt="";
if (document.implementation && document.implementation.createDocument) {
if (xmlDoc.documentElement.nodeName=="parsererror") {
var obj=xmlDoc.documentElement;
txt=obj.childNodes[0].data+"\n";
txt+=obj.childNodes[1].nodeName+":"+obj.childNodes[1].childNodes[0].nodeValue;
alert(txt);
} else {
alert("No error found");
}
} else { //ie-specific
if(xmlDoc.parseError.errorCode!=0) {
txt="Error Code: " + xmlDoc.parseError.errorCode + "\n";
txt+="Error Reason: " + xmlDoc.parseError.reason;
txt+="Error Line: " + xmlDoc.parseError.line;
document.forms["rule"].validxml.focus();
alert(txt);
} else {
alert("No errors found");
}
}
}
[/tt]
Format of error message txt is somewhat arbitrary, and you can do whatever it looks better to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top