Validate an XML Document Against an XML Schema Using C++

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Schemas

Validate an XML Document Against an XML Schema Using C++

To validate an XML document file with an XML Schema definition language (XSD) schema file using C++, you load XML and XSD documents and create a schema cache. The following example illustrates this procedure.

#include "stdio.h"

#import <5.0.dll>
using namespace MSXML2;

int checkParseError(IXMLDOMParseErrorPtr pError);
void dump_com_error(_com_error &e);


int main(int argc, char* argv[])
{
   
   CoInitialize(NULL);
   try{
      
      IXMLDOMParseErrorPtr  pError;
      
      // load the XML file
      // ****** you need to use IXMLDOMDocument2 interface *********
      IXMLDOMDocument2Ptr pXMLDoc;
      HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument50));
      pXMLDoc->async =  VARIANT_FALSE; 
      
      hr = pXMLDoc->load("books.xml");      
      
      //check on the parser error      
      if(hr!=VARIANT_TRUE)
      {
         return checkParseError(pXMLDoc->parseError);
      }
      
      //load the XSD file
      IXMLDOMDocumentPtr pXSDDoc;
      hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument50));
      pXSDDoc->async =  VARIANT_FALSE; 
      
      hr = pXSDDoc->load("books.xsd");
      
      //check on the parser error      
      if(hr!=VARIANT_TRUE)
      {         
         return checkParseError(pXSDDoc->parseError);
      }
      
      //create schemacache
      IXMLDOMSchemaCollectionPtr pSchemaCache;
      hr = pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache50));
      pXMLDoc->schemas = pSchemaCache.GetInterfacePtr();
      
      //hook it up with XML Document
      hr = pSchemaCache->add("urn:books", pXSDDoc.GetInterfacePtr());   
      
      //call validate 
      pError = pXMLDoc->validate();
      
      if(pError->errorCode != S_OK)
      {
         _bstr_t parseError = _bstr_t("Error code: ")+ _bstr_t(pError->errorCode) +_bstr_t("\n") + _bstr_t("Reason: ")+ pError->Getreason();
         MessageBox(NULL, (char*)parseError, "Parse Error",MB_OK);
         return -1;
      }
      else
         MessageBox(NULL,"Valiation succeeded", "Results",MB_OK);
      
   }
   catch(_com_error &e)
   {
      dump_com_error(e);
   }
   return 0;
}


int checkParseError(IXMLDOMParseErrorPtr pError)
{
   _bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline()) + _bstr_t("\n")+ _bstr_t(pError->Getreason());
   MessageBox(NULL,parseError, "Parse Error",MB_OK);
   return -1;
   
}

void dump_com_error(_com_error &e)
{
   printf("Error\n");
   printf("\a\tCode = %08lx\n", e.Error());
   printf("\a\tCode meaning = %s", e.ErrorMessage());
   _bstr_t bstrSource(e.Source());
   _bstr_t bstrDescription(e.Description());
   printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
   printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}

Input file: books.xml

<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books">
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</x:catalog>

Input file: books.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:b="urn:books">

<xsd:element name="catalog" type="b:CatalogData"/>
<xsd:complexType name="CatalogData">
<xsd:sequence>
<xsd:element name="book" type="b:bookdata" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="bookdata">
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float"/>
<xsd:element name="publish_date" type="xsd:date"/>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

See Also

Validate an XML Document or Fragment | XML Schema Examples | XML Schema Element Map | XML Schema Elements | XML Data Types Reference | Primitive XML Data Types | Derived XML Data Types | Data Type Facets | IXMLDOMSchemaCollection/XMLSchemaCache

Other Resources Other Resources

World Wide Web Consortium (W3C) XML Schema Part 2: Datatypes | World Wide Web Consortium (W3C) XML Schema Part 1: Structures