HOW TO: Use SAX to Validate XML Data in Visual C++ (311423)
The information in this article applies to:
This article was previously published under Q311423 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 FileThe following file is available for download from the Microsoft Download Center:
The Saxxsdvalid.exe file contains the following files: |
Po.xml | 1K | Po.xsd | 3K | Saxerrorhandlerimpl.cpp | 2K | Saxerrorhandlerimpl.h | 2K | Saxxsd.cpp | 2K | Saxxsd.dsp | 5K | Saxxsd.dsw | 1K | Saxxsderrorhandler.h | 2K | Saxxsderrorhandler.cpp | 2K | Stdafx.cpp | 1K | Stdafx.h | 2K |
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"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())));
- 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;
- 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:
- 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.
Modification Type: | Minor | Last Reviewed: | 8/10/2004 |
---|
Keywords: | kbdownload kbfile kbhowto kbHOWTOmaster KB311423 kbAudDeveloper |
---|
|