您的位置:首页 > 其它

基于ATL使用MSXML2生成XML文件

2010-11-10 19:37 471 查看
#include <atlbase.h>
#include <MsXml2.h>
#include <atlconv.h>

void main(int argc,char*argv[])
{
CoInitialize(NULL);

CComPtr<IXMLDOMDocument> spDoc;
spDoc.CoCreateInstance(__uuidof(DOMDocument));

CComPtr<IXMLDOMElement> spRootElement;
spDoc->createElement(L"China",&spRootElement);
LPWSTR lpwAttri = L"+86";
USES_CONVERSION;
CComVariant varAttri = T2OLE(lpwAttri);
spRootElement->setAttribute(L"id",varAttri);
CComPtr<IXMLDOMNode> spChildNode;
spDoc->appendChild(spRootElement,&spChildNode);

if(spChildNode) spChildNode.Release();
CComPtr<IXMLDOMElement> spChildElement;
spDoc->createElement(L"BeiJing",&spChildElement);
spRootElement->appendChild(spChildElement,&spChildNode);
spChildNode->put_text(L"010");

if(spChildElement) spChildElement.Release();
if(spChildNode) spChildNode.Release();
spDoc->createElement(L"ShangHai",&spChildElement);
spRootElement->appendChild(spChildElement,&spChildNode);
spChildNode->put_text(L"020");

CComVariant varFile = L"D://test.xml";
spDoc->save(varFile);

spChildElement.Release();
spChildNode.Release();
spRootElement.Release();
spDoc.Release();

CoUninitialize();
}


 

生成的XML:

- <China id="+86">
<BeiJing>010</BeiJing>
<ShangHai>020</ShangHai>
</China>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xml include null