#include "stdafx.h"
#include <string>
#import <msxml.dll> named_guids
using namespace std;
using namespace MSXML;
int _tmain(int argc, _TCHAR* argv[])
{
::OleInitialize(NULL);
MSXML::IXMLDOMDocumentPtr pDoc; //xml文档指针
MSXML::IXMLDOMElementPtr xmlRoot; //根节点
MSXML::IXMLDOMElementPtr xmlStat; //第一子节点
MSXML::IXMLDOMElementPtr xmlDetail; //第二子节点
HRESULT hr = pDoc.CreateInstance(MSXML::CLSID_DOMDocument);
if(FAILED(hr))
{
_com_error er(hr);
printf("%s\n", er.ErrorMessage());
}
xmlRoot = pDoc->createElement("report"); //根节点名
xmlStat = pDoc->createElement("stat"); //第一子节点名
xmlDetail = pDoc->createElement("detail"); //第二子节点名
//stat下的file节点
for (int i=0;i<10;i++)
{
MSXML::IXMLDOMElementPtr xmlFile; //第一子节点下的file节点
MSXML::IXMLDOMElementPtr pNode; //元素
string strSuffixName = ".exe";
string strCount = "1232";
xmlFile = pDoc->createElement("file");
//suffix元素
pNode=pDoc->createElement("suffix"); //元素名
pNode->Puttext(strSuffixName.c_str()); //元素值
xmlFile->appendChild(pNode);
//count元素
pNode=pDoc->createElement("count");
pNode->Puttext(strCount.c_str());
xmlFile->appendChild(pNode);
xmlStat->appendChild(xmlFile);
pNode = NULL;
xmlFile = NULL;
}
//detail下的file节点
for (int i=0;i<10;i++)
{
MSXML::IXMLDOMElementPtr xmlFile; //第二子节点下的file节点
MSXML::IXMLDOMElementPtr pNode; //元素
string strFileName = "Excel.exe";
string strPath = "D:\\TestSystemManage\\Code\\";
string strSize = "37212b";
xmlFile = pDoc->createElement("file");
//suffix元素
pNode=pDoc->createElement( "name" );
pNode->Puttext(strFileName.c_str());
xmlFile->appendChild(pNode);
//path元素
pNode=pDoc->createElement("path");
pNode->Puttext(strPath.c_str() );
xmlFile->appendChild(pNode);
//size元素
pNode=pDoc->createElement("size");
pNode->Puttext(strSize.c_str());
xmlFile->appendChild(pNode);
xmlDetail->appendChild(xmlFile);
pNode = NULL;
xmlFile = NULL;
}
xmlRoot->appendChild(xmlStat);
xmlRoot->appendChild(xmlDetail);
pDoc->appendChild(xmlRoot);
pDoc->save("D:\\a.xml");
return 0;
}
vc++ 写xml
最新推荐文章于 2023-12-06 18:13:50 发布