XML validation with parseFromString on Safari

J

jul

Hi,

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 ?

Thanks

Julien
 
P

pr

jul said:
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 <parsererror> element 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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top