Input Validation in XMLToCSVBasic.cpp

As I noted, the default behavior of the load method is to validate while parsing. So, if we haven't specified validation on the command line, we want to disable validation while loading. We do that by setting the validateOnParse property of the DOM Document to false.

spDocInput->validateOnParse = VARIANT_FALSE;

But what should we do if we want to validate? Do we perform validation while parsing via the load method, or do we just load without validation and then call validate? The quick answer is “both.” We want to validate while parsing and call the validate method. Why both calls? The main reason is that the validate method does not report as much error information in the ParseError object as does the load method. So, we want to use load, with the default behavior of validating while parsing, as our primary validation tool. However, the load method by itself is not sufficient. In my testing I found that load was unable to report an error in cases where either there was no schema specified or there was a problem with the schema. As a result, for the most reliable validation we want to call both methods. Since we don't need to do anything to change the default behavior of load, we only need to add the call to validate. Here's the code to add to XMLToCSVBasic.cpp.

Validation Code in XMLToCSVBasic.cpp
//  Validate the input document
if (boValidate)
{
  spParseError = spDocInput->validate();
  if( spParseError->errorCode != S_OK)
  {
    cerr << "Validation Error" << endl;
    displayParseError(spParseError);
    throw cValidationError;
  }
}

The only real trick here is that while the load operation returns just an HRESULT (you need to explicitly call the Document's getParseError), the validate method returns an IXMLDOMParseError. We pass that to the displayParseError routine and then throw an exception to exit the try block. Validation errors typically show only reason text and error codes, not any other useful information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset