简体   繁体   English

从字节数组将XML加载到C ++ MSXML中

[英]Load XML into C++ MSXML from byte array

I'm receiving XML over a network socket. 我正在通过网络套接字接收XML。 I need to take that XML and load it into the DOM to perform further operations. 我需要获取该XML并将其加载到DOM中以执行进一步的操作。 MSXML requires input strings that are in either UCS-2 or UTF-16 and completely ignores the XML header with the encoding type when loading from a string. MSXML要求输入字符串在UCS-2或UTF-16中,并且从字符串加载时完全忽略具有编码类型的XML标头。 It allows the loading of XML fragments, so this makes some sense. 它允许加载XML片段,因此这很有意义。

I see two possible ways to handle this problem: 我看到两种可能的方法来解决此问题:

1) Write the file out to disk and load it into MSXML via file paths. 1)将文件写出到磁盘,然后通过文件路径将其加载到MSXML中。 The extra disk I/O makes this approach far from preferred. 额外的磁盘I / O使这种方法远非首选。

2) Peak into the XML header to manually detect the encoding and then call MultiByteToWideChar to convert into UTF-16 and specify the code page based on the detected encoding. 2)进入XML标头以手动检测编码,然后调用MultiByteToWideChar转换为UTF-16并根据检测到的编码指定代码页。 This approach works OK, but I'd like to push the encoding detection onto MSXML. 这种方法行得通,但是我想将编码检测推到MSXML上。

Does anybody have any other ideas on how to accomplish this? 是否有人对实现此目标有其他想法?

I haven't looked at other XML parsers, but would be interested in how non-MSXML DOM parsers accomplish this. 我没有看过其他XML解析器,但会对非MSXML DOM解析器如何完成此工作感兴趣。

Thanks, Paul 谢谢保罗

Simplest way is pass the load function a safe array. 最简单的方法是将加载函数传递给安全数组。 eg 例如

const char* xml = "<root/>";

SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = strlen(xml);

SAFEARRAY* psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
memcpy(psa->pvData, xml, strlen(xml));
VARIANT v;

VariantInit(&v);
V_VT(&v) = VT_ARRAY | VT_UI1;
V_ARRAY(&v) = psa;
VARIANT_BOOL fSuccess;
pXMLDoc->load(v, &fSuccess);
if(fSuccess == VARIANT_TRUE)
{
    /* Do Something */
}

Obviously no error checking going on or freeing of resources. 显然,没有错误检查正在进行或释放资源。

Or use CreateStreamOnHGlobal to create an IStream on the data and pass that into load. 或使用CreateStreamOnHGlobal在数据上创建一个IStream并将其传递给负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM