Hi all,
Is it possible to check if loaded with xmlDoc.loadXML(xmlData); xml string is invalid? For example if there is missed closing bracket or a tag.
Hi all,
Is it possible to check if loaded with xmlDoc.loadXML(xmlData); xml string is invalid? For example if there is missed closing bracket or a tag.
If you pass a string to loadXML
that isn't a well-formed XML document, the document object will be empty (no childNodes) and xmlDoc.parseError.errorCode
will be set to something other than 0
. xmlDoc.parseError.reason
will give you a user-readable error message.
If you want to test a snippet and not a full document, wrap it in <x>
...</x>
tags so that the parser will only see one root element.
(There are a few reasons that MSXML might fail to parse a document other than it being non-well-formed. For example an external DTD subset or entity might not be network-reachable, or the DTD might use features MSXML doesn't support. You can't use MSXML to parse XHTML documents with their DTD for this reason. But if DTD-cruft isn't involved, a parser failure means the input wasn't well-formed.)
I figured it out by myself and w3cschool :) All info about parse errors is hidden in "xmlDoc.parseError"