I've been trying to figure out the msxml parser by adapting some examples to C.
With the following code I would expect the line:
wprintf(L"XML = %ls\n", sxml);
to print some xml to the console but all I get is the following:
XML =
Any ideas about what I'm doing wrong?
In the project I specify the following on the linker tab:
kernel32.lib advapi32.lib delayimp.lib Ole32.lib Uuid.lib msxml2.lib oleaut32.lib
And here's the snippet:
#ifndef _M_IX86
#define _M_IX86
#endif
#define WIN32_LEAN_AND_MEAN /* speed up compilations */
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
//MSXML support
#define COBJMACROS
#include <objbase.h>
#include <msxml6.h>
LPWSTR ShowError(IXMLDOMDocument *pXMLDocument)
{
static WCHAR sError[MAX_PATH];
memset(&sError,0,sizeof(sError));
IXMLDOMParseError *errorObj;
IXMLDOMDocument_get_parseError(pXMLDocument, &errorObj);
BSTR reasonString = NULL;
IXMLDOMParseError_get_reason(errorObj, &reasonString);
wprintf(sError,L"MSXML::DomDocument::load failed. \nError: %ls",
NULL != reasonString ? reasonString : L"");
return sError;
}
int main(int argc, char *argv[])
{
//Create the XML
IXMLDOMDocument *pXMLDoc;
do
{
VARIANT_BOOL loaded;
//Initialize the COM library on the current thread
HRESULT hr = CoInitialize(NULL);
//Create a single uninitialized object of the class associated with CLSID_DOMDocument30
hr = CoCreateInstance(&CLSID_DOMDocument30, NULL,
CLSCTX_INPROC_SERVER, &IID_IXMLDOMDocument, (void**)&pXMLDoc);
if (FAILED(hr))
{
wprintf(L"Failed to create the XML class instance\n");
break;
}
BSTR xml = SysAllocString(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
"<Parent Depth=\"0\">" \
"<Child1 Depth=\"1\">This is a child of Parent<//Child1>" \
"<Child2 Depth=\"1\">" \
"<Child3 Depth=\"2\">" \
"<Child4 Depth=\"3\">This is a child of Child3<//Child4>" \
"<//Child3>" \
"<//Child2>" \
"<//Parent>");
if(VARIANT_FALSE == IXMLDOMDocument_loadXML(pXMLDoc, xml, &loaded))
{
wprintf(L"%s",ShowError(pXMLDoc));
break;
}
SysFreeString(xml);
BSTR sxml = NULL;
IXMLDOMDocument_get_xml(pXMLDoc,&sxml);
wprintf(L"XML = %ls\n", sxml);
}while(FALSE);
return 0;
}