首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用CComPtr<IXMLDOMDocument> (C++)保存XML头

用CComPtr<IXMLDOMDocument> (C++)保存XML头
EN

Stack Overflow用户
提问于 2012-08-25 05:06:31
回答 1查看 1.6K关注 0票数 1

我使用atl和CComPtr,如下所示:

代码语言:javascript
复制
CComPtr<IXMLDOMDocument> xml_doc;
HRESULT hr = xml_doc.CoCreateInstance(__uuidof(DOMDocument))
...
HRESULT hr = xml_doc->createElement(CComBSTR(element_name.c_str()), &element);
...
etc

当我调用xml_doc->save时,它会保存没有标题的xml。添加标题的最简单方法是什么?

EN

回答 1

Stack Overflow用户

发布于 2012-08-25 05:54:42

已解决:

代码语言:javascript
复制
HRESULT hr;

CComPtr<IXMLDOMProcessingInstruction> proc_instr;
hr = xml_out->createProcessingInstruction(L"xml", L"version=\"1.0\" encoding=\"UTF-8\"", &proc_instr);
if (FAILED(hr))
    throw std::exception("Failed to create IXMLDOMDocument processing instruction");


CComPtr<IXMLDOMNode> first_child;
hr = xml_out->get_firstChild(&first_child);
if (FAILED(hr))
    throw std::exception("Failed to get first child of IXMLDOMDocument");

hr = xml_out->insertBefore(proc_instr, CComVariant(first_child), NULL);
if (FAILED(hr))
    throw std::exception("Failed to insert IXMLDOMDocument processing instruction");


hr = xml_out->save(CComVariant(header->output_file.c_str()));
if (FAILED(hr))
    throw std::exception("Failed to save response XML to '" + header->output_file + "'");

http://msdn.microsoft.com/en-us/library/windows/desktop/ms755439(v=vs.85).aspx

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12116657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档