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

Article translations Article translations
Article ID: 311423 - View products that this article applies to.
This article was previously published under Q311423
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page


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

Discussion of the Saxxsdvalid.exe File

The following file is available for download from the Microsoft Download Center:
The Saxxsdvalid.exe file contains the following files:

Collapse this tableExpand this table
File nameSize
Saxxsd.dsp 5K

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:
  1. 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;
                //Add a schema to the schema cache.
                CHR(pSchemaCache->add(L"", _variant_t("po.xsd")));  
                //Turn on the validation feature.
                //Receive all validation errors.
                CHR(pReader->putFeature(L"exhaustive-errors", VARIANT_TRUE));
                //Associate the schema cache with the SAX reader.
                CHR(pReader->putProperty(L"schemas", _variant_t(pSchemaCache.GetInterfacePtr())));
  2. 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"\n %ld \n line : %ld column : %ld\n  %s\n", hrErrorCode,m, n, pwchErrorMessage);
         // Return S_OK to continue the parsing.
         return S_OK;
  3. Outputs the fatal error in the fatal error event handler of the SAXXSDErrorHandler class, as follows:
         wprintf(L"\n fatal Error %ld \n line : %d column : %d\n  %s\n", hrErrorCode,pLocator->getLineNumber, pLocator->getColumnNumber, pwchErrorMessage);
         return hrErrorCode;

Use SAX to Validate XML Data

To use SAX to validate XML data, follow these steps:
  1. Unzip the Saxxsdvalid.exe file.
  2. Compile and run the application. Validation errors such as the following are printed on the console window:
    Parsing document: po.xml
    line : 1 column : 79
    Required attribute 'confirmDate' is missing.

    line : 29 column : 16
    Element content is invalid according to the DTD/Schema.
    Expecting: {}comment.


Article ID: 311423 - Last Review: February 27, 2014 - Revision: 2.1
  • Microsoft XML Core Services 4.0
kbnosurvey kbarchive kbdownload kbfile kbhowto kbhowtomaster KB311423

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from