none
如果使用XML SDK处理nameingspace? RRS feed

  • 常规讨论

  • 操作系统: windows XP home版本
    XML SDK: 4.0
    开发工具: Visual C++ 6

    #include "stdafx.h"
    #include <stdio.h>
    #include <windows.h>
    #include <locale.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");

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


    int main(int argc, char* argv[])
    {
       IXMLDOMDocument2 *pXMLDom=NULL;
       IXMLDOMParseError *pXMLErr=NULL;
       BSTR bstr = NULL;
       BSTR xmlStr = NULL;
       VARIANT_BOOL status;
       IXMLDOMNodeList *pNodes=NULL;
       IXMLDOMNode *pNode=NULL;


       VARIANT p1;
       VARIANT p2;
       VariantInit(&p1);
       V_BSTR(&p1) = SysAllocString(L"xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'");

       VariantInit(&p2);
       V_BSTR(&p2) = SysAllocString(L"xmlns:nns_0='http://www.primeton.com/com.primeton.eos.wfclient/com/primeton/eos/wfclient/wfclientservice'");

     

       CoInitialize(NULL);
        setlocale(LC_ALL,"chinese-simplified");
       pXMLDom = DomFromCOM();
       if (!pXMLDom) goto clean;


       pXMLDom->setProperty(BSTR("SelectionNamespaces"), p1);   // 使用了这个方法来注册命名空间,但是好像没起作用
       pXMLDom->setProperty(BSTR("SelectionNamespaces"), p2);

       // HRCALL(pXMLDom->load(var, &status), "");
       //xmlStr= SysAllocString(L"<?xml version='1.0'?><soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body><ns_0:startWorkFlowOutMsg xmlns:ns_0='http://www.primeton.com/com.primeton.eos.wfclient/com/primeton/eos/wfclient/wfclientservice' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type='ns_0:startWorkFlowOutMsg'><ns_0:result>63</ns_0:result></ns_0:startWorkFlowOutMsg></soapenv:Body></soapenv:Envelope>"); 
    xmlStr = SysAllocString(L"<?xml version='1.0'?><a>xiaojing</a>");

     pXMLDom->loadXML(xmlStr, &status);
     pXMLDom->get_xml(&bstr);

     //bstr = SysAllocString(L"/Envelope/Body/startWorkFlowOutMsg/result");
     bstr = SysAllocString(L"/a");

     pXMLDom->selectSingleNode(bstr, &pNode);
     //HRCALL(pNode->get_xml(&bstr), "get_xml: ");
     pNode->get_text(&bstr);
        dprintf("\t%S\n\n", bstr);


      
    clean:
       if (bstr) SysFreeString(bstr);
       if (pXMLErr) pXMLErr->Release();
       if (pXMLDom) pXMLDom->Release();

       CoUninitialize();
       return 1;
    }

     

    2009年7月22日 9:45