C-C++ Code (validateNode.cpp)

 

#include <stdio.h>
#include "msxml6.h"

void dump_com_error(_com_error &e);
void show_parse_error(_bstr_t ctx, MSXML2::IXMLDOMParseErrorPtr pErr);

int main(int argc, char* argv[])
{
   IXMLDOMDocument3Ptr          pXMLDoc  = NULL;
   IXMLDOMDocument3Ptr          pXSDDoc  = NULL;
   IXMLDOMSchemaCollectionPtr   pSCache  = NULL;
   MSXML2::IXMLDOMNodePtr       pNode    = NULL;
   MSXML2::IXMLDOMNodeListPtr   pNodelist= NULL;
   MSXML2::IXMLDOMParseErrorPtr pError   = NULL;
   HRESULT                      hr;
   long                         i        = 0;
   long                         x        = 0;

   CoInitialize(NULL);
   try{
     // Load validateNode.xml into a DOM instance.
      hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument60));
      if (FAILED(hr)) {
        MessageBox(NULL, "Cannot create DOMDocument60 xml", 
                  "CreateInstance", MB_OK);
         return 0;
     }
      pXMLDoc->async           =  VARIANT_FALSE;       
      pXMLDoc->validateOnParse =  VARIANT_FALSE;
      if(pXMLDoc->load("validateNode.xml") != VARIANT_TRUE)
      {
         show_parse_error(_bstr_t("Can't load validateNode.xml\n"), 
                          pXMLDoc->parseError);
         return 0;
      }

     // Load validateNode.xsd into a DOM instance.
      hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument60));
      if (FAILED(hr)) {
        MessageBox(NULL, "Cannot create DOMDocument60 for xsd",
                  "CreateInstance", MB_OK);
         return 0;
     }
      pXSDDoc->async           =  VARIANT_FALSE;       
      pXSDDoc->validateOnParse =  VARIANT_FALSE;
      if(pXSDDoc->load("validateNode.xsd") != VARIANT_TRUE)
      {
         show_parse_error(_bstr_t("Can't load validateNode.xsd\n"), 
                          pXSDDoc->parseError);
         return 0;
      }

      // Create a schema cache.
     hr = pSCache.CreateInstance(__uuidof(XMLSchemaCache60));
      if (FAILED(hr)) {
        MessageBox(NULL, "Cannot create XMLSchemaCache60", 
                  "CreateInstance", MB_OK);
         return 0;
     }

     // Point the XML doc's schema to the loaded schema cache.
      pXMLDoc->schemas = pSCache.GetInterfacePtr();

     hr = pSCache->add("urn:books", pXSDDoc.GetInterfacePtr());
      if (FAILED(hr)) {
        MessageBox(NULL, "Cannot add schema", "add", MB_OK);
         return 0;
     }

     // Validate the entire DOM object.
      pError =pXMLDoc->validate();
      if (pError->errorCode != 0) {
         show_parse_error(_bstr_t("invalid dom:\n"), pError);
     }
      else
         MessageBox(NULL,pXMLDoc->xml, "import",MB_OK);

     // Validate all book nodes, node by node.
      pNodelist = pXMLDoc->selectNodes("//book");
      x = pNodelist->length;
      for(i = 1; i <= x; i++)
      {
         pNode = pNodelist->nextNode();
         pError = pXMLDoc->validateNode(pNode);
         if (pError->errorCode != 0)
         {
             show_parse_error(_bstr_t("invalid node:\n"), pError);
         }
         else
            MessageBox(NULL,pNode->xml, "validateNode",MB_OK);
      }
   }
   catch(_com_error &e)
   {
      dump_com_error(e);
   }
   pXMLDoc.Release();
   pXSDDoc.Release();
   pSCache.Release();
   pNode.Release();
   pNodelist.Release();
   pError.Release();
   CoUninitialize();
   return 0;
}

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);
}

void show_parse_error(_bstr_t ctx, MSXML2::IXMLDOMParseErrorPtr pError)
{
    _bstr_t parseError = ctx +  _bstr_t(pError->Getreason());
    MessageBox(NULL,parseError, "Parse Error",MB_OK);   
}

Try It!

  1. Start Visual C++.

  2. From the File menu, select New. On the Projects tab of the New dialog box that appears, select Win32 Console Application in the left pane. Then type "validateNodeProj" in the Project name field. For the project Location field, either accept the default setting or choose another location. Click OK.

  3. The Win32 Console Application property page will appear. For the type of Console Application, select An empty project and click Finish. When the New Project Information box displays, click OK.

  4. Select FileView on the project browser, and highlight validateNodeProj files. From the File menu, select New.

  5. On the Files tab of the New dialog box, highlight C++ Source File. Then type "validateNode.xml" in the File name text box.

  6. Click OK.

  7. Copy the XML data file (validateNode.xml), and paste it into the text file you just created.

  8. Repeat steps 4-7 for the XSD listing (validateNode.xsd).

    Note

    You can also copy the file into the project's main directory using Windows Explorer (or a command prompt).

  9. Repeat steps 4-7 for the C++ file above (validateNode.cpp).

  10. Build the sample by selecting Build validateNodeProj.exe from the Build menu.

  11. Execute the sample application by selecting !Execute validateNodeProj.exe from the Build menu.

  12. Verify that the result is the same as that listed in Output for the validateNode Example.