使用MSXML2生成XML文件

#include
#import "C:\windows\system32\msxml2.dll"

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

   MSXML2::IXMLDOMDocumentPtrpDoc;
  pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));

   MSXML2::IXMLDOMElementPtrpRootNode;
   pRootNode =pDoc->createElement_x(L"China");
  pRootNode->setAttribute(L"id",L"+86");
  pDoc->appendChild(pRootNode);


   MSXML2::IXMLDOMNodePtrpChildNode;
   pChildNode =pDoc->createElement_x(L"BeiJing");
  pChildNode->put_text(L"010");
  pRootNode->appendChild(pChildNode);

   pChildNode =pDoc->createElement_x(L"ShangHai");
  pChildNode->put_text(L"020");
  pRootNode->appendChild(pChildNode);

  pDoc->save(L"D:\\test.xml");

   pChildNode.Release();
   pRootNode.Release();
   pDoc.Release();

   CoUninitialize();
}

 

生成的内容:

- < Chinaid =" +86 ">
    < BeiJing > 010 BeiJing >
    < ShangHai > 020 ShangHai >
China >

你可能感兴趣的:(C++)