开发者

Load XML into C++ MSXML from byte array

I'm receiving XML over a network socket. I need to take that XML and load it into the DOM to perform further operations. MSXML requires input strings that are in either UCS-2 or UTF-16 and completely ignores the XML h开发者_开发知识库eader with the encoding type when loading from a string. It allows the loading of XML fragments, so this makes some sense.

I see two possible ways to handle this problem:

1) Write the file out to disk and load it into MSXML via file paths. The extra disk I/O makes this approach far from preferred.

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. This approach works OK, but I'd like to push the encoding detection onto 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.

Thanks, Paul


Simplest way is pass the load function a safe array. e.g.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜