Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Sunday, October 16, 2005

validate XML with xsd via MSXML

///////////////////////empty_element.js

// create XML : <ParticipantObjectDescription><Accession Number=""/></ParticipantObjectDescription>

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");

var root;

var newElem;

xmlDoc.async = false;

xmlDoc.loadXML("<ParticipantObjectDescription></ParticipantObjectDescription>");

if (xmlDoc.parseError.errorCode != 0) {

   var myErr = xmlDoc.parseError;

   WScript.Echo("You have error " + myErr.reason);

} else {

   root = xmlDoc.documentElement;

   newElem = xmlDoc.createElement("Accession");

   root.appendChild(newElem);

   var attributes = newElem.attributes;

    var number = xmlDoc.createAttribute("Number");

    //number.value = "";

    attributes.setNamedItem(number);

   //WScript.Echo(root.xml);

}



/////validate xml by xsd

if(false)

{

    var xmlDoc   =  new ActiveXObject("Msxml2.DOMDocument.4.0");

    var xsdCache = new ActiveXObject("Msxml2.XMLSchemaCache.4.0");

    xsdCache.add(" ","empty_element.xsd"); 

    xmlDoc.async   = false;

    xmlDoc.schemas =  xsdCache; 

    xmlDoc.load("empty_element.xml");

    var err = xmlDoc.validate();

    if (err.errorCode == 0){

        WScript.Echo("Document is valid");

    }else{

        WScript.Echo("Validation error:" + err.reason);

    } 

}



var sOutput = validateFile("empty_element.xml");

//sOutput = sOutput + validateFile("sc-notValid.xml");

WScript.Echo(sOutput);



function validateFile(strFile)

{

    // Create a schema cache and add books.xsd to it.

    var xs = new ActiveXObject("MSXML2.XMLSchemaCache.4.0");

    xs.add("urn:books", "empty_element.xsd");

    // Create an XML DOMDocument object.

    var xd = new ActiveXObject("MSXML2.DOMDocument.4.0");

    // Assign the schema cache to the DOMDocument's

    // schemas collection.

    xd.schemas = xs;



    // Load books.xml as the DOM document.

    xd.async = false;

    xd.validateOnParse = true;

    xd.resolveExternals = true;

    xd.load(strFile);



    // Return validation results in message to the user.

    if (xd.parseError.errorCode != 0)

    {

         return("Validation failed on " + strFile +

                "\n=====================" +

                "\nReason: " + xd.parseError.reason +

                "\nSource: " + xd.parseError.srcText +

                "\nLine: " + xd.parseError.line + "\n");

    }

    else

         return("Validation succeeded for " + strFile +

                "\n======================\n" +

                xd.xml + "\n");

}



////////////////empty_element.xsd

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

<xsd:element name="ParticipantObjectDescription" type="ParticipantObjectDescriptionType"/>

<xsd:complexType name="ParticipantObjectDescriptionType">

    <xsd:sequence>

            <xsd:element name="Accession" minOccurs="1" maxOccurs="unbounded">

                <xsd:complexType>

                    <xsd:attribute name="Number" type="xsd:string" use="optional"/>

                </xsd:complexType>

            </xsd:element>

    </xsd:sequence>

</xsd:complexType>

</xsd:schema>



////////////////////empty_element.xml

<?xml version="1.0"?>

<ParticipantObjectDescription>

    <Accession Number=""/>

    <Accession Number="111"/>

    <Accession/>

</ParticipantObjectDescription>

Saturday, October 15, 2005

read table of Access into CString array via MFC

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once


#include <iostream>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS     // some CString constructors will be explicit

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN          // Exclude rarely-used stuff from Windows headers
#endif

#include <afx.h>
#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include

<afxdtctl.h>
         // MFC support
for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include
<afxcmn.h>          
    // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

// tmpmfcconsole.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "tmpmfcconsole.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include <afxdb.h>
// The one and only application object

CWinApp theApp;

using namespace std;
class  CDBVariantEx : public CDBVariant
{
public:
     void GetStringValue(LPSTR lpsz);
     void GetStringValue(CString& rstrValue);
};
void CDBVariantEx::GetStringValue(LPSTR lpsz)
{
     switch(m_dwType)
     {
     case DBVT_STRING:
          sprintf(lpsz, "%s", m_pstring->GetBuffer(m_pstring->GetLength()));
          break;

     case DBVT_LONG:
          sprintf(lpsz, "%ld", m_lVal);
          break;

     case DBVT_DOUBLE:
          sprintf(lpsz, "%f", m_dblVal);
          break;

     case DBVT_SHORT:
          sprintf(lpsz, "%d", m_iVal);
          break;

     case DBVT_NULL:
          sprintf(lpsz, "%s", _T("NULL"));
          break;

     case DBVT_BOOL:
          if (TRUE == m_boolVal) sprintf(lpsz, "%s", _T("True"));
          else sprintf(lpsz, "%s", _T("False"));
          break;
     case DBVT_DATE:
          {
     

         sprintf(lpsz,"%d-%02d-%02d
%02d:%02d:%02d.%03d",m_pdate->year,m_pdate->month,
     
         
    m_pdate->day,m_pdate->hour,m_pdate->minute,m_pdate->second,m_pdate->fraction);
          }
          break;
     case DBVT_ASTRING:
          {
               strcpy(lpsz,  *m_pstringA);
               break;
          }
     default:
          sprintf(lpsz, _T("type:%d"),m_dwType);
          break;
     }
}

void CDBVariantEx::GetStringValue(CString& rstrValue)
{
     switch(m_dwType)
     {
     case DBVT_STRING:
     

    rstrValue.Format("%s",
m_pstring->GetBuffer(m_pstring->GetLength()));
          break;

     case DBVT_LONG:
          rstrValue.Format("%ld", m_lVal);
          break;

     case DBVT_DOUBLE:
          rstrValue.Format("%f", m_dblVal);
          break;

     case DBVT_SHORT:
          rstrValue.Format("%d", m_iVal);
          break;

     case DBVT_NULL:
          rstrValue.Format("%s", _T(""));
          break;

     case DBVT_BOOL:
          if (TRUE == m_boolVal) rstrValue.Format("%s", _T("True"));
          else rstrValue.Format("%s", _T("False"));
          break;
     case DBVT_DATE:
          {
     

         rstrValue.Format("%d-%02d-%02d
%02d:%02d:%02d.%03d",m_pdate->year,m_pdate->month,
     
         
    m_pdate->day,m_pdate->hour,m_pdate->minute,m_pdate->second,m_pdate->fraction);
          }

          break;

     default:
          rstrValue.Format(_T("type:%d"),m_dwType);
          break;
     }
}


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
     int nRetCode = 0;
     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
     {
          _tprintf(_T("Fatal Error: MFC initialization failed\n"));
          nRetCode = 1;
     }
     else
     {
          //sample code to read MDB table into CString array
          //build by Onega(www.fruitfruit.com)
          //VC++ 2003, Windows XP, Access 2003
          //warning: use it at your own risk.
          //More error checking, TCHAR handling is required
          const int NUMCOLUMNS = 8;
          const int MAXNUMRECORDS = 8;
          CString strAccessTable[MAXNUMRECORDS][NUMCOLUMNS] ;
          CDatabase db;
          db.OpenEx("Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\onega.mdb;");
          CRecordset rs(&db);
          rs.Open(AFX_DB_USE_DEFAULT_TYPE,"select * From table1");
          int nfieldcount=rs.GetODBCFieldCount();
          CString msg;
          msg.Format("There are %d fields in table1",nfieldcount);
          AfxMessageBox(msg);
          int row = 0;
          while(!rs.IsEOF())
          {
               for(short i=0;i<nfieldcount;i++)
               {
     

         
    CDBVariantEx dbvar;
     
         
    rs.GetFieldValue(i,dbvar);
     
         
    char buf[1024] = {0};
     
         
    dbvar.GetStringValue(buf);
     
         
    strAccessTable[row][i] = buf;
     
         
    printf("%02d,%02d
%s\n",row,i,strAccessTable[row][i]);
               }
               rs.MoveNext();
               row++;
               if(row>=MAXNUMRECORDS)
                    break;
          }
          rs.Close();
          db.Close();
     }
     system("pause");
     return nRetCode;
}