Click here to Skip to main content
Click here to Skip to main content

XML by Schema

, 29 Sep 2002
Rate this:
Please Sign up or sign in to vote.
Validate XML with XML schema.

Sample Image - sample.jpg

Description

This application is used to validate XML files against XML schemas. This requires the MSXML library (4.0 or above); be aware of the bugs and update to the latest version or Service Pack!

Usage

The Collection

Is a storage where you can add <namespace, schema> pairs (check MSDN on the subject of XML schemas and IXMLDOMSchemaCollection for more information). Basically, you provide a namespace related to a schema. The namespace will be unique in the collection while the schema should appear in several distinct pairs.

Status

Take a look at this box after every operation, to see if it succeeded or not.

Insert Pair

Write the URN of the schema in the uppermost edit box. You can open it from the local machine, pressing Open. Write the namespace in its edit place (the combo). Press Insert into Collection. Notice that the namespace could be an empty string or one made entirely by spaces. Inserting a namespace that already exists in the collection will update the content of the schema associated. You have to do it any time you modify the schema file. If you fail to insert a schema for a namespace that already exists in the collection, the last successfully inserted will remain as active.

Remove Pair

Is done using only the namespace. Type it or choose it from the combo and press Remove from Collection.

Remove All Pairs

Just press Empty entire Collection.

Validate XML

Write the URN of the XML file in the uppermost edit box or Open it. Press Validate as XML.

The Browser

Is used to display the content of the files involved into the validation process. You can also use it as with IE (view and modify the source, for example).

IXMLDOMSchemaCollection As Used

The code is from ValidatorDlg.cpp.

Creation

BOOL CValidatorDlg::OnInitDialog()
{
  // ...
  HRESULT hr2, hr1=m_doc.CoCreateInstance(__uuidof(DOMDocument));
  hr2=m_schema.CoCreateInstance(__uuidof(XMLSchemaCache));
  if (hr1!=S_OK || hr2!=S_OK)
  {
    MessageBox("Creating COM objects failure!\r\n"
      "Make sure you have installed MSXML 4.0 on your machine!",
      "XMLbyXSD");
    PostQuitMessage(0);
  }
  VARIANT var;
  var.vt = VT_DISPATCH;
  var.pdispVal = m_schema.p;
  if (m_doc->putref_schemas(var)!=S_OK) // associate schema with document
  {
    MessageBox("COM operation failed!", "XMLbyXSD");
    PostQuitMessage(0);
  }
  m_doc->put_async(VARIANT_FALSE);
  m_doc->put_validateOnParse(VARIANT_TRUE);
  // ...
}

Pair Insertion

void CValidatorDlg::OnButtonAdd() 
{
  CString szMan, szUrn;
  // ...
  try
  {
    // adding <namespace, schema> pair to collection
    m_schema->add(_bstr_t(szMan), _variant_t(szUrn)); 
    // ...
  }
  catch (_com_error &x) // attempt failed!  
  {
    PrintStatus("Schema error!", (char *)x.Description(), 
      0, NULL); 
    return;
  }
}

Namespace Removing

void CValidatorDlg::OnButtonRemove() 
{
  CString szMan;
  // ...
    try
    {
      // remove namespace+schema from collection
      m_schema->remove(_bstr_t(szMan)); 
    }
    catch (_com_error &x) // attempt failed
    {
      PrintStatus("Removing schema error!", (char *)x.Description(), 
        0, NULL);
      return;
    }
  // ...
}

Validation

void CValidatorDlg::OnButtonValidate() 
{
  BSTR bs;
  CString szReason, szText;
  // ...
  // associated schema collection will accomplish its task
  m_doc->load(_variant_t(szText)); 
  IXMLDOMParseError *pErr=NULL;
  if (m_doc->get_parseError(&pErr)==S_OK)
    if (pErr)
    {
      pErr->get_errorCode(&ln);
      if (ln) // errors
      {
        // ...
      }
      else
        PrintStatus("XML is valid according to "
        "collection content!", NULL, ln, NULL);
      pErr->Release();
    }
}

Updates

  • September 30, 2002 - Bug fixed.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hans Ruck
Software Developer (Senior)
Europe Europe
No Biography provided

Comments and Discussions

 
GeneralIXMLDOMSchemaCollection::add() may fail() PinmemberAaron Heusser7-Sep-04 13:12 
GeneralRe: IXMLDOMSchemaCollection::add() may fail() PinmemberBogdan Rechi7-Sep-04 23:29 
GeneralRe: IXMLDOMSchemaCollection::add() may fail() PinmemberAaron Heusser8-Sep-04 5:07 
No worries! The purpose of my posting was not to criticize ... rather to inform (at least some other people ... even if you already knew). I had to figure this out "the hard way" and wanted to clarify some things that could potentially cause others to think that your app doesn't work.
 
Thanks for posting the article in the first place!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Sep 2002
Article Copyright 2002 by Hans Ruck
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid