vc++ 写xml

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值