基于ATL使用MSXML2生成XML文件

#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,null,include)