Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate xml schema using msxml parser

Tags:

c++

xsd

msxml

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.

like image 659
Uday Avatar asked Feb 10 '26 21:02

Uday


1 Answers

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.

like image 102
Cheeso Avatar answered Feb 13 '26 14:02

Cheeso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!