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

This article was previously published under Q311423
This article has been archived. It is offered "as is" and will no longer be updated.
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:
The Saxxsdvalid.exe file contains the following files:

File nameSize
Po.xml1K
Po.xsd3K
Saxerrorhandlerimpl.cpp2K
Saxerrorhandlerimpl.h2K
Saxxsd.cpp2K
Saxxsd.dsp 5K
Saxxsd.dsw1K
Saxxsderrorhandler.h2K
Saxxsderrorhandler.cpp2K
Stdafx.cpp1K
Stdafx.h2K


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;            CHR(pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache40)));            //Add a schema to the schema cache.            CHR(pSchemaCache->add(L"http://www.example.org/po", _variant_t("po.xsd")));              //Turn on the validation feature.            CHR(pReader->putFeature(L"schema-validation",VARIANT_TRUE));            //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);     printf("\n");     // 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;					
back to the top

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
    -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.
xdr dtd Saxxsdvalid
Properties

Article ID: 311423 - Last Review: 02/27/2014 21:10:35 - Revision: 2.1

  • Microsoft XML Core Services 4.0
  • kbnosurvey kbarchive kbdownload kbfile kbhowto kbhowtomaster KB311423
Feedback