Microsoft KB Archive/311423

= HOW TO: Use SAX to Validate XML Data in Visual C++ =

Article ID: 311423

Article Last Modified on 6/21/2007

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q311423



IN THIS TASK
SUMMARY
 * Discussion of the Saxxsdvalid.exe File
 * Use SAX to Validate XML Data



SUMMARY
This step-by-step article describes how to use Simple API for XML (SAX) to validate XML data in Microsoft Visual C++.

back to the top

Discussion of the Saxxsdvalid.exe File
The following file is available for download from the Microsoft Download Center:

Saxxsdvalid.exe

The Saxxsdvalid.exe file contains the following files:

Saxxsdvalid.exe demonstrates how to use SAX to validate XML data with XSD schema and to receive all validation errors in Visual C++. This sample implements the ISAXErrorHandler interface to receive errors that the SAX reader generates.

Saxxsdvalid.exe does the following:   Creates a schema cache object and sets appropriate flags with the SAX reader in the main function, as follows: //Create the schema cache. IXMLDOMSchemaCollectionPtr pSchemaCache; CHR(pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache40)));

//Add a schema to the schema cache. CHR(pSchemaCache->add(L&quot;http://www.example.org/po&quot;, _variant_t(&quot;po.xsd&quot;)));

//Turn on the validation feature. CHR(pReader->putFeature(L&quot;schema-validation&quot;,VARIANT_TRUE));

//Receive all validation errors. CHR(pReader->putFeature(L&quot;exhaustive-errors&quot;, VARIANT_TRUE));

//Associate the schema cache with the SAX reader. CHR(pReader->putProperty(L&quot;schemas&quot;, _variant_t(pSchemaCache.GetInterfacePtr)));

  Outputs the error to the console window in the error event handler of the SAXXSDErrorHandler class, as follows: int m, n;    HRESULT hr = pLocator->getLineNumber(&m); hr = pLocator->getColumnNumber(&n);

wprintf(L&quot;\n %ld \n line : %ld column : %ld\n %s\n&quot;, hrErrorCode,m, n, pwchErrorMessage);

printf(&quot;\n&quot;);

// Return S_OK to continue the parsing. return S_OK;

  Outputs the fatal error in the fatal error event handler of the SAXXSDErrorHandler class, as follows: wprintf(L&quot;\n fatal Error %ld \n line : %d column : %d\n %s\n&quot;, hrErrorCode,pLocator->getLineNumber, pLocator->getColumnNumber, pwchErrorMessage);

return hrErrorCode; 

back to the top

Use SAX to Validate XML Data
To use SAX to validate XML data, follow these steps:  Unzip the Saxxsdvalid.exe file. Compile and run the application. Validation errors such as the following are printed on the console window:

Parsing document: po.xml

-2147467259

line : 1 column : 79

Required attribute 'confirmDate' is missing.

-2147467259

line : 29 column : 16

Element content is invalid according to the DTD/Schema.

Expecting: {http://www.example.org/po}comment.

</ol>

Additional query words: xdr dtd Saxxsdvalid

Keywords: kbdownload kbfile kbhowto kbhowtomaster KB311423

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.