Jump to content

Who will generate error if wrong value is entered in XML Doc.

Recommended Posts

Hi,I am very new to XML Schema. I have initiated learning XML Schema few days back. I know XML Schema and its usages. But, I am not getting as "Who will throw error if value between element are entered wrong base on XML Schema"E.g.If user enter digits in <firstName> </firstName> element (where string is expected), then who will throw error. Either, IE or high level language (java) or...?reply wud be hearthly appreciated...Thank you...

Share this post

Link to post
Share on other sites

Whatever application performs the validation. IE can do schema validation with JavaScript if the user has installed MSXML6, which is not that common, so its best not to rely on JavaScript to perform any schema validation.The server side scripting language (S3L) you use must have a schema validator on it's own. That validator itself will be responsible for performing the validation and throwing an error if you're wrong.For example, in PHP, there's the [ur=http://bg.php.net/manual/en/function.dom-domdocument-schemavalidate.php]DOMDocument::schemaValidate()[/url] function that does the validation, and with the libxml functions, you can check for the exact error messages, if any.Here's an example:

<?php$xml = 'source.xml'; //The XML file that will be validated$schema = 'schema.xsd'; //The schema to validate the XML file againstlibxml_use_internal_errors(true); /* Provide access to the rest of the libxml functions and disable error display. We'll handle errors on our own */$dom = new DOMDocument;if ($dom->load($xml)) {	echo "$xml is not well formed. The following parsing errors occured:";	foreach (libxml_get_errors() as $error) {		echo "{$error->message} on line {$error->line}\n";	}if ($dom->schemaValidate($schema)) {	echo "$xml is valid";}else {	echo "$xml is not valid. The following errors occured:";	foreach (libxml_get_errors() as $error) {		echo "{$error->message} on line {$error->line}\n";	}}?>

You may of course write a more sophisticated and customized validator on your own. That's only a generic, plain text validator that doesn't even use up all error handlers PHP provides.In the case of every other language (JAVA included), you must use that language's schema validator and error handlers. I'm not knowledgeable in JAVA, so I can't even begin to suggest how to do it there.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...