NO

Author Topic: msxml snippet does not produce expected output  (Read 4512 times)

Offline DMac

  • Member
  • *
  • Posts: 272
msxml snippet does not produce expected output
« on: May 22, 2014, 02:29:01 AM »
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:
Code: [Select]
wprintf(L"XML = %ls\n", sxml);to print some xml to the console but all I get is the following:
Code: [Select]
XML =
Any ideas about what I'm doing wrong?

In the project I specify the following on the linker tab:
Code: [Select]
kernel32.lib advapi32.lib delayimp.lib Ole32.lib Uuid.lib msxml2.lib oleaut32.lib
And here's the snippet:
Code: [Select]
#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;
}
No one cares how much you know,
until they know how much you care.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: msxml snippet does not produce expected output
« Reply #1 on: May 22, 2014, 09:11:15 AM »
There is errors in xml-string like <//Child1> instead </Child1> ?
return value from COM function is 0 S_OK not VARIANT_FALSE ?
Info from here

Tip: external xml-file loading
Code: [Select]
...
        VARIANT vxml;
        vxml.vt = VT_BSTR;
        vxml.bstrVal = SysAllocString(L"test1.xml");
        if(IXMLDOMDocument_load(pXMLDoc, vxml, &loaded))
        {
            SysFreeString(vxml.bstrVal);
            wprintf(L"%ls",ShowError(pXMLDoc));
            break;
        }
        SysFreeString(vxml.bstrVal);
...
May the source be with you

czerny

  • Guest
Re: msxml snippet does not produce expected output
« Reply #2 on: May 22, 2014, 09:15:36 AM »
Code: [Select]
            "<//Parent>");
I am not very familiar with XML. But why do you use two slashes in front of your closing tags?

czerny

  • Guest
Re: msxml snippet does not produce expected output
« Reply #3 on: May 22, 2014, 09:17:36 AM »
The debugger gets cofused with linenumbers in this example! Can you confirm this?

Offline DMac

  • Member
  • *
  • Posts: 272
Re: msxml snippet does not produce expected output
« Reply #4 on: May 22, 2014, 07:52:51 PM »
There is errors in xml-string like <//Child1> instead </Child1> ?
return value from COM function is 0 S_OK not VARIANT_FALSE ?
Info from here

Ah Ha!
I should not have trusted the initial example. >:(  The VARIANT_FALSE yields the opposite result of what I would have expected.  (I don't know how the other guy's code even worked now.  Maybe it never did.  :o )

Not questioning this line resulted in a brief descent into madness.  The method appeared to approve of malformed xml and reject proper xml!  ???  My addled brain reasoned "The example used raw string literals so I must escape the slashes and I forward escaped all forward slashes and the method seemed to approve." :'(

I was lost in the woods and stumbling around like a beginner.  I thought "maybe Timo can point me the way out of here."  Thanks Timo.   :)

The debugger gets cofused with linenumbers in this example! Can you confirm this?

Most of these method calls are actually convenience macros that the pre-processor expands into COM syntax.  I am guessing that this causes a discrepancy between the visible source I want to step through and the compiled code the debugger hooks into.

This reminds me of the years I lived and taught in Ukraine.  Everything appeared to be one thing on the surface when in reality it was wholly another thing once one looked deeper into the matter.  And that also included me myself!   ;)

I find that if I place multiple break points where I want to step the code things will line up on them.
No one cares how much you know,
until they know how much you care.