我正在尝试将格式化的 XML 文档从 DOM 对象输出到字符串中,如下面的代码所示。问题是,虽然 dom->get_xml() 方法确实返回了一个值,但 MXXMLWriter->output 没有,正如您在屏幕截图中看到的那样。所有方法调用都返回 S_OK。这里可能是什么问题?
这是我的代码:
CComPtr<IMXWriter> w;
HRESULT hr;
hr = w.CoCreateInstance(__uuidof(MXXMLWriter), NULL, CLSCTX_INPROC_SERVER);
hr = w->put_indent(VARIANT_TRUE);
CComPtr<ISAXXMLReader> rd;
hr = rd.CoCreateInstance(__uuidof(SAXXMLReader60), NULL, CLSCTX_INPROC_SERVER);
CComPtr<ISAXContentHandler> hc;
hr = w.QueryInterface<ISAXContentHandler>(&hc);
CComPtr<ISAXErrorHandler> he;
hr = w.QueryInterface<ISAXErrorHandler>(&he);
CComPtr<ISAXDTDHandler> hd;
hr = w.QueryInterface<ISAXDTDHandler>(&hd);
hr = rd->putContentHandler(hc);
hr = rd->putErrorHandler(he);
hr = rd->putDTDHandler(hd);
hr = rd->putProperty(L"http://xml.org/sax/properties/lexical-handler", CComVariant(w));
hr = rd->putProperty(L"http://xml.org/sax/properties/declaration-handler", CComVariant(w));
BSTR body = ::SysAllocStringLen(NULL, 1024);
ZeroMemory(body, 1024 * sizeof(OLECHAR));
CComVariant out(body);
w->put_encoding(CComBSTR(L"utf-8"));
hr = w->put_output(out);
hr = rd->parse(CComVariant(dom));
dom->get_xml(&xmlStr);
return std::wstring(xmlStr);