传递SAFEARRAY的字节来加载MSXML的功能

时间:2017-10-14 05:02:13

标签: c++ xml windows msxml safearray

如果有任何类型的灵魂,请通过以下来源告诉我为什么MSXML“加载”功能无法加载此XML。

这里我试图使用MSXML解析器的“加载”功能加载UTF-8编码的XML。我有一个BSTR [UTF-16编码]作为参数,所以我试图将其转换为SAFEARRAY的字节,以便我可以将它传递到MSXML的“加载”功能。但问题是加载功能无法加载此XML。如果有人能提供解决方案,我将非常感激。

#include <windows.h>
#include <objsafe.h>
#include <objbase.h>
#include <atlbase.h>
#include <string>
#include <comutil.h>
#include <msxml2.h>
#include <iostream>

using namespace std;

#define STATUS_SUCCESS 0
#define STATUS_FAIL -1

long LoadXmlData(BSTR xmlDoc)
{
HRESULT hr = S_OK;
CComPtr <IXMLDOMDocument> xmlDomDoc = NULL;
CComPtr <IXMLDOMElement> docRoot = NULL;
VARIANT_BOOL isParseSucess = FALSE;

CoInitialize(NULL);
hr = xmlDomDoc.CoCreateInstance(__uuidof(DOMDocument30));
if (FAILED(hr))
{
    return STATUS_FAIL;
}

BYTE HUGEP *pByte;
int len = WideCharToMultiByte(CP_UTF8, 0, xmlDoc, -1, NULL, 0, NULL, NULL);
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].cElements = len;
rgsabound[0].lLbound = 0;

SAFEARRAY* psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
if (psa != NULL)
{
    hr = SafeArrayAccessData(psa, (void HUGEP**)&pByte);
    if (!FAILED(hr))
    {
        if (len > 0)
        {
            WideCharToMultiByte(CP_UTF8, 0, xmlDoc, -1, (LPSTR)&pByte[0], len, NULL, NULL);
            //cout << "Converted Byte Array: " << pByte << endl << endl;
        }
        else
        {
            return STATUS_FAIL;
        }
        SafeArrayUnaccessData(psa);
    }
}

VARIANT v;
VariantInit(&v);
V_VT(&v) = VT_ARRAY | VT_UI1;
V_ARRAY(&v) = psa;

hr = xmlDomDoc->load(v, &isParseSucess);
//hr = xmlDomDoc->loadXML(xmlDoc, &isParseSucess); //can't use this function because XML is encoded in UTF-8

if (FAILED(hr) || (!isParseSucess))
{
    return STATUS_FAIL;
}
else
{
    return STATUS_SUCCESS;
}
}

int main()
{
BSTR xmlDoc = SysAllocString(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?> <response> </response> ");
long ret = LoadXmlData(xmlDoc);
if (ret == STATUS_SUCCESS)
{
    cout << "MSXML: loading the XML succeeded";
}
else
{
    cout << "MSXML: loading the XML failed";
}
//string str;
//getline(cin, str);
return 0;
}

ps:如果有人尝试编译此源,您可能会第一次收到链接错误,在VS设置中添加comsuppw.lib作为链接器依赖项。而且XML是UTF-8编码的,所以我不能在MSXML中使用“LoadXML”功能。

1 个答案:

答案 0 :(得分:1)

XML文件是UTF-8,但这里不需要转换,因为Windows函数会在需要时自动在UTF-16和UTF-8之间进行转换(除非输入/输出是BYTE* ...)

BSTR需要清理。您可以使用具有自动清理功能的CComBSTR(str)

您可以使用const wchar_t*将字符串传递给您自己的函数。 COM等需要BSTR

如评论中所述,IXMLDOMDocument::load期望文件名作为输入。在这种情况下使用IXMLDOMDocument::loadXML

#include <iostream>
#include <windows.h>
#include <atlbase.h>
#include <msxml2.h>

long LoadXmlData(const wchar_t* content)
{
    HRESULT hr = S_FALSE;
    CComPtr<IXMLDOMDocument> doc = NULL;
    CComPtr<IXMLDOMElement> docRoot = NULL;

    hr = doc.CoCreateInstance(__uuidof(DOMDocument30));
    if(FAILED(hr))
        return S_FALSE;

    VARIANT_BOOL is_success = FALSE;
    CComBSTR arg(content);
    hr = doc->loadXML(arg, &is_success);

    //if you are loading from a file:
    //hr = doc->load(CComVariant(L"c:\\test\\test.xml"), &is_success);

    if(FAILED(hr) || !is_success)
        return S_FALSE;

    //if save is needed:
    //doc->save(CComVariant(L"c:\\test\\test.xml"));
    return S_OK;
}

int main()
{
    CoInitialize(NULL);

    //ελληνική γλώσσα Greek text for testing
    CComBSTR data(L"<?xml version=\"1.0\" encoding=\"UTF-8\"?><response>ελληνική γλώσσα</response>");
    long ret = LoadXmlData(data);
    if(ret == S_OK)
        std::cout << "MSXML: loading the XML succeeded\n";
    else
        std::cout << "MSXML: loading the XML failed\n";

    CoUninitialize();
    return 0;
}