473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

MSXML5.0:Validate an XML Docxumenet (C++)-Fatal Error C1010

SHC
Hi all,
I have a VC++ .NET 2003 - Windows XP Pro PC. I created a Win32 console
application in my VC++ .NET 2003 and copied validateDOM.cpp, books.xml and
books.xsd (see the attached files below) from Microfost MSDN Library for my
project "validateXML". When I did "Build" on my project, I got the following
fatal error C1010:
c:\Documents and Settings\SHC\My Documents\Visual Studio
Projects\valoidateDOM\valoidateDOM.cpp(273): fatal error C1010: unexpected
end of file while looking for precompiled header directive.
Please help and tell me what went wrong in my "Build" and How I can correct
the problem.
SHC
///////////-----validateDOM.cpp------/////////////
// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include <stdio.h>
#include <windows.h>
#import <msxml4.dll> raw_interfaces_only
using namespace MSXML2;

// Macro that calls a COM method returning HRESULT value:
#define HRCALL(a, errmsg) \
do { \
hr = (a); \
if (FAILED(hr)) { \
dprintf( "%s:%d HRCALL Failed: %s\n 0x%.8x = %s\n", \
__FILE__, __LINE__, errmsg, hr, #a ); \
goto clean; \
} \
} while (0)

// Helper function that put output in stdout and debug window
// in Visual Studio.
void dprintf( char * format, ...)
{
static char buf[1024];
va_list args;
va_start( args, format );
vsprintf( buf, format, args );
va_end( args);
OutputDebugStringA( buf);
printf("%s", buf);
}

// Helper function to create a DOM instance:
IXMLDOMDocument2 * DomFromCOM()
{
HRESULT hr;
IXMLDOMDocument2 *pxmldoc = NULL;

HRCALL( CoCreateInstance(__uuidof(DOMDocument40),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLDOMDocument),
(void**)&pxmldoc),
"Create a new DOMDocument");

HRCALL( pxmldoc->put_async(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_validateOnParse(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_resolveExternals(VARIANT_FALSE),
"should never fail");
HRCALL( pxmldoc->put_preserveWhiteSpace(VARIANT_TRUE),
"should never fail");

return pxmldoc;
clean:
if (pxmldoc)
{
pxmldoc->Release();
}
return NULL;
}

// Helper function packaging a BSTR into a variant:
VARIANT VariantString(BSTR str)
{
VARIANT var;
VariantInit(&var);
V_BSTR(&var) = SysAllocString(str);
V_VT(&var) = VT_BSTR;
return var;
}

// Helper function packaging an object into a variant:
VARIANT VariantObject(IUnknown *pUnk)
{
VARIANT var;
HRESULT hr;
IDispatch *pDisp=NULL;

VariantInit(&var);
HRCALL(pUnk->QueryInterface(IID_IDispatch,(void**)&pDisp),"" );
var.pdispVal = pDisp;
var.vt = VT_DISPATCH;
return var;
clean:
VariantClear(&var);
return var;
}

// Helper function to display parse error:
void ReportParseError(IXMLDOMDocument *pDom, char *desc) {
IXMLDOMParseError *pXMLErr=NULL;
BSTR bstrReason = NULL;
HRESULT hr;
HRCALL(pDom->get_parseError(&pXMLErr),
"dom->get_parseError: ");
HRCALL(pXMLErr->get_reason(&bstrReason),
"parseError->get_reason: ");

dprintf("%s %S\n",desc, bstrReason);
clean:
if (pXMLErr) pXMLErr->Release();
if (bstrReason) SysFreeString(bstrReason);
}

// Validate XML document as a whole.
void ValidateDocument(IXMLDOMDocument2 *pDom) {

IXMLDOMParseError *pErr=NULL;
BSTR bstr = NULL;
HRESULT hr;
long eCode;

if (!pDom) {
dprintf("Can't validate document. Invalid DOM\n");
return;
}

dprintf("Validating DOM...\n");
HRCALL(pDom->validate(&pErr), "");
HRCALL(pErr->get_errorCode(&eCode), "");
if (eCode != 0) {
HRCALL(pErr->get_reason(&bstr), "");
dprintf("\tXMLDoc is not valid because\n%S\n",bstr);
SysFreeString(bstr);
bstr = NULL;
}
else {
HRCALL(pDom->get_xml(&bstr),"");
dprintf("\tXMLDoc is validated: \n%S\n",bstr);
SysFreeString(bstr);
bstr = NULL;
}
clean:
if (pErr) pErr->Release();
if (bstr) SysFreeString(bstr);
}

// Validate Document nodes, node by node.
void ValidateDocumentNodes(IXMLDOMDocument3 *pDom, BSTR xpath)
{
IXMLDOMNode * pNode = NULL;
IXMLDOMNodeList *pNodelist = NULL;
IXMLDOMParseError *pError = NULL;
BSTR bstr = NULL;
long length, eCode, i;
HRESULT hr;

if (!pDom) {
dprintf("Can't validate document nodes. Invalid DOM\n");
return;
}

HRCALL(pDom->selectNodes(xpath,&pNodelist),"");
HRCALL(pNodelist->get_length(&length), "");
for (i=0; i<length; i++) {
HRCALL(pNodelist->get_item(i, &pNode), "");
HRCALL(pDom->validateNode(pNode, &pError),"");
HRCALL(pError->get_errorCode(&eCode), "");
HRCALL(pNode->get_nodeName(&bstr), "");
if (eCode != 0 ) {
BSTR bstr1 = NULL;
HRCALL(pError->get_reason(&bstr1), "");
dprintf("\t<%S> (%d) is not valid because\n%S\n",
bstr, i, bstr1);
SysFreeString(bstr1);
bstr1=NULL;
}
else {
dprintf("\t<%S> (%d) is a valid node\n",bstr,i);
}
SysFreeString(bstr);
bstr=NULL;
}
clean:
if (bstr) SysFreeString(bstr);
if (pError) pError->Release();
if (pNode) pNode->Release();
if (pNodelist) pNodelist->Release();
return;
}
int main(int argc, char* argv[])
{
IXMLDOMDocument2 *pXMLDoc = NULL;
IXMLDOMDocument2 *pXSDDoc = NULL;
IXMLDOMSchemaCollection *pSCache = NULL;
BSTR bstr = NULL;
VARIANT_BOOL status;
VARIANT var;
HRESULT hr;

CoInitialize(NULL);
VariantInit(&var);

// Create a DOm and load a document from books.xml
pXMLDoc = DomFromCOM();
if (!pXMLDoc) goto clean;

VariantClear(&var);
var = VariantString(L"books.xml");
HRCALL(pXMLDoc->load(var, &status), "");
if (status!=VARIANT_TRUE) {
ReportParseError(pXMLDoc,
"Failed to load DOM from books.xml");
goto clean;
}

// Create a Dom and load a schema from books.xsd.
pXSDDoc = DomFromCOM();
if (!pXSDDoc) goto clean;

VariantClear(&var);
var = VariantString(L"books.xsd");
HRCALL(pXSDDoc->load(var, &status), "");
if (status!=VARIANT_TRUE) {
ReportParseError(pXSDDoc,
"Failed to load DOM from books.xsd");
goto clean;
}

// Create a schema collection object.
HRCALL( CoCreateInstance(__uuidof(XMLSchemaCache50),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IXMLDOMSchemaCollection),
(void**)&pSCache),
"Create a new Schema collection object");

// Add schema to the schema collection.
bstr = SysAllocString(L"urn:books");
VariantClear(&var);
var = VariantObject(pXSDDoc);
HRCALL(pSCache->add(bstr,var), "add schema");
SysFreeString(bstr);
bstr = NULL;
VariantClear(&var);

// Attaching the schema to the XML document.
var = VariantObject(pSCache);
HRCALL(pXMLDoc->putref_schemas(var),"");
VariantClear(&var);

// Validate the document as a whole.
ValidateDocument(pXMLDoc);

// Validate all //book nodes, node by node.
bstr = SysAllocString(L"//book");
ValidateDocumentNodes(pXMLDoc, bstr);
SysFreeString(bstr);
bstr = NULL;

// Validate all //book/* nodes, node by node.
bstr = SysAllocString(L"//book/*");
ValidateDocumentNodes(pXMLDoc, bstr);
SysFreeString(bstr);
bstr = NULL;

clean:
if (bstr) SysFreeString(bstr);
if (&var) VariantClear(&var);
if (pXMLDoc) pXMLDoc->Release();
if (pXSDDoc) pXSDDoc->Release();
if (pSCache) pSCache->Release();

CoUninitialize();
return 0;
}
///////-------bokks.xml--------//////////
<?xml version="1.0"?>
<x:books xmlns:x="urn:books">
<book id="bk001">
<author>Writer</author>
<title>The First Book</title>
<genre>Fiction</genre>
<price>44.95</price>
<pub_date>2000-10-01</pub_date>
<review>An amazing story of nothing.</review>
</book>

<book id="bk002">
<author>Poet</author>
<title>The Poet's First Poem</title>
<genre>Poem</genre>
<price>24.95</price>
<review>Least poetic poems.</review>
</book>
</x:books>

//////----books.xsd------//////
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:books"
xmlns:bks="urn:books">

<xsd:element name="books" type="bks:BooksForm"/>

<xsd:complexType name="BooksForm">
<xsd:sequence>
<xsd:element name="book"
type="bks:BookForm"
minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="BookForm">
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="genre" type="xsd:string"/>
<xsd:element name="price" type="xsd:float" />
<xsd:element name="pub_date" type="xsd:date" />
<xsd:element name="review" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>

Nov 12 '05 #1
0 4550

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: aevans1108 | last post by:
Greetings All If this is the wrong place to post this question, please give me a push in the right direction. Thanks. I know there has to be a simpler way to do this, but this is as simple a...
3
by: Claire | last post by:
I have a large record with many enumerated fields. The record is stored in a file and the fields have to be extracted. I validate the data as it's read, but there's so many tests similar to the...
7
by: Ali-R | last post by:
Hi all, I am getting a CSV file like this from our client: "C1","2","12344","Mr","John","Chan","05/07/1976"......... I need to validate **each filed value** against a set of rules ,for...
5
by: Jim Heavey | last post by:
When should you use the Page.Validate() method? I thought you would use this method if you have some Server side validation (CustomControl's) you wanted to use and this would cause them to be...
4
by: Brybot | last post by:
I have a form that i've split up into multiple asp:panels, each panel has a number of validators which work correctly. At on the last panel, i want to commit the data collected to a database. I...
24
by: Mike Hofer | last post by:
Please forgive the cross-post to multiple forums. I did it intentionally, but I *think* it was appropriate given the nature of my question. I'm working on an open source code library to help...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.