3

I want to validate an XML file against an XML Schema file. It is a simple xml file, does not include namespace etc. I want to do this in c++, using MSXML 6.0.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Uday
  • 819
  • 3
  • 12
  • 25

1 Answers1

4

You can validate as you load. This is sample code from the Windows/MSXML SDK:

   IXMLDOMSchemaCollectionPtr   pXS;
   IXMLDOMDocument2Ptr          pXD = NULL;
   IXMLDOMParseErrorPtr         pErr = NULL;
   _bstr_t                      strResult = "";

   HRESULT hr = pXS.CreateInstance(__uuidof(XMLSchemaCache50));
   hr = pXS->add("urn:namespace", "myschema.xsd");

   // Create a DOMDocument and set its properties.
   hr = pXD.CreateInstance(__uuidof(DOMDocument50));

   // Assign the schema cache to the DOMDocument's
   // schemas collection.
   pXD->schemas = pXS.GetInterfacePtr();

   // Load books.xml as the DOM document.
   pXD->async = VARIANT_FALSE;
   pXD->validateOnParse = VARIANT_TRUE;
   pXD->resolveExternals = VARIANT_TRUE;
   hr = pXD->load("TheXmlDocument.xml");

   // check hr and pXD->errorCode here

You can download the MSXML6 SDK to get this sample and lots of others. Note: It will not install on Vista. If you run Vista, then get the Windows SDK.

Cheeso
  • 189,189
  • 101
  • 473
  • 713