jul wrote:
When I use parseFromString on Firefox or loadXML on Internet Explorer,
I can know if it's a valid xml content but when I use parseFromString
on Safari, I get a part of the content until it's invalid but I can't
know if the content is valid or not.
Someone has any idea ?
Unless I've missed something, Firefox's DOMParser doesn't validate XML
against a DTD or schema, so I'm thinking you mean 'well-formed' rather
than 'valid'.
In the case of both Firefox and Safari, you can determine whether a
string parsed successfully by the presence of a <parsererrorelement in
the resulting XML document. They differ about where you find it and what
namespace it is in, yet you can still do something simple like this:
var s, x, p = new DOMParser();
s = "<my-xml>" +
"<ok>content</ok>" +
"<rubbish>&</rubbish>" +
"</my-xml>";
// & is an error - remove it to make result OK
x = p.parseFromString(s, "text/xml");
alert(x.getElementsByTagName("parsererror").length ?
"xml error" : "ok!");
You could use x.evaluate() in place of x.getElementsByTagName().