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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章