简体   繁体   中英

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 header 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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