tinyxml demo

#include <iostream>
#include "tinyxml/tinyxml.h"
#include "tinyxml/tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>
#include <map>

using namespace std;

CStringA GetAppPath()
{//获取应用程序根目录
	CHAR modulePath[MAX_PATH];
	GetModuleFileNameA(NULL, modulePath, MAX_PATH);
	CStringA strModulePath(modulePath);
	strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
	return strModulePath;
}
struct MyDevice
{
	string usb_insert_time;
	string	usb_pullout_time;
	string	usb_name;
	string	guid;
	string	vendorid;
	string	productid;
	string	android_version;
	string	android_model;
	string	android_vendor;
	string	is_have_root;
	string	install_type;
	string	install_result;
	string	start_service_result;
	string	computer_id;
	string	device_id;
};
struct OtherDevice{
	map<string,string> datas;

};


/*
devices
device

usb_name
usb_insert_time
usb_pullout_time
guid

vendorid
productid
android_version
android_model

android_vendor
is_have_root
install_type
install_result

start_service_result
computer_id
*/

bool CreateXmlFile2(string& szFileName,OtherDevice* otherDevice)
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
	try
	{
		//创建一个XML的文档对象。
		TiXmlDocument *myDocument = new TiXmlDocument();
		//创建一个根元素并连接。
		TiXmlElement *RootElement = new TiXmlElement("Devices");
		myDocument->LinkEndChild(RootElement);
		//创建一个Person元素并连接。
		TiXmlElement *deviceElement = new TiXmlElement("Device");
		RootElement->LinkEndChild(deviceElement);

		map<string,string>::iterator it;
		for(it=otherDevice->datas.begin();it!=otherDevice->datas.end();++it)
		{
			string key=it->first;
			string value=it->second;
			if(!key.empty() && !value.empty())
			{
				TiXmlElement *element = new TiXmlElement(key.c_str());
				element->LinkEndChild(new TiXmlText(value.c_str()));
				deviceElement->LinkEndChild(element);
			}
			
		}
		CStringA appPath = GetAppPath();
		string seperator = "\\";
		string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
		myDocument->SaveFile(fullPath.c_str());//保存到文件
	}
	catch (...)
	{
		return false;
	}
	return true;
}
bool CreateXmlFile(string& szFileName,MyDevice* myDevice)
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
	try
	{
		//创建一个XML的文档对象。
		TiXmlDocument *myDocument = new TiXmlDocument();
		//创建一个根元素并连接。
		TiXmlElement *RootElement = new TiXmlElement("Devices");
		myDocument->LinkEndChild(RootElement);
		//创建一个Person元素并连接。
		TiXmlElement *deviceElement = new TiXmlElement("Device");
		RootElement->LinkEndChild(deviceElement);
		//设置Person元素的属性。
		deviceElement->SetAttribute("computer_id", myDevice->computer_id.c_str());
		//创建name元素、age元素并连接。
		TiXmlElement *usb_nameElement = new TiXmlElement("usb_name");
		TiXmlElement *usb_insert_timeElement = new TiXmlElement("usb_insert_time");
		TiXmlElement *usb_pullout_timeElement = new TiXmlElement("usb_pullout_time");
		TiXmlElement *guidElement = new TiXmlElement("guid");

		TiXmlElement *vendoridElement = new TiXmlElement("vendorid");
		TiXmlElement *productidElement = new TiXmlElement("productid");
		TiXmlElement *android_versionElement = new TiXmlElement("android_version");
		TiXmlElement *android_modelElement = new TiXmlElement("android_model");

		TiXmlElement *android_vendorElement = new TiXmlElement("android_vendor");
		TiXmlElement *is_have_rootElement = new TiXmlElement("is_have_root");
		TiXmlElement *install_typeElement = new TiXmlElement("install_type");
		TiXmlElement *install_resultElement = new TiXmlElement("install_result");
		TiXmlElement *start_service_resultElement = new TiXmlElement("start_service_result");



		usb_nameElement->LinkEndChild(new TiXmlText(myDevice->usb_name.c_str()));
		usb_insert_timeElement->LinkEndChild(new TiXmlText(myDevice->usb_insert_time.c_str()));
		usb_pullout_timeElement->LinkEndChild(new TiXmlText(myDevice->usb_pullout_time.c_str()));
		guidElement->LinkEndChild(new TiXmlText(myDevice->guid.c_str()));

		vendoridElement->LinkEndChild(new TiXmlText(myDevice->vendorid.c_str()));
		productidElement->LinkEndChild(new TiXmlText(myDevice->productid.c_str()));
		android_versionElement->LinkEndChild(new TiXmlText(myDevice->android_version.c_str()));
		android_modelElement->LinkEndChild(new TiXmlText(myDevice->android_model.c_str()));

		android_vendorElement->LinkEndChild(new TiXmlText(myDevice->android_vendor.c_str()));
		is_have_rootElement->LinkEndChild(new TiXmlText(myDevice->is_have_root.c_str()));
		install_typeElement->LinkEndChild(new TiXmlText(myDevice->install_type.c_str()));
		install_resultElement->LinkEndChild(new TiXmlText(myDevice->install_result.c_str()));
		start_service_resultElement->LinkEndChild(new TiXmlText(myDevice->start_service_result.c_str()));


		deviceElement->LinkEndChild(usb_nameElement);
		deviceElement->LinkEndChild(usb_insert_timeElement);
		deviceElement->LinkEndChild(usb_pullout_timeElement);
		deviceElement->LinkEndChild(guidElement);

		deviceElement->LinkEndChild(vendoridElement);
		deviceElement->LinkEndChild(productidElement);
		deviceElement->LinkEndChild(android_versionElement);
		deviceElement->LinkEndChild(android_modelElement);

		deviceElement->LinkEndChild(android_vendorElement);
		deviceElement->LinkEndChild(is_have_rootElement);
		deviceElement->LinkEndChild(install_typeElement);
		deviceElement->LinkEndChild(install_resultElement);
		deviceElement->LinkEndChild(start_service_resultElement);


		CStringA appPath = GetAppPath();
		string seperator = "\\";
		string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
		myDocument->SaveFile(fullPath.c_str());//保存到文件
	}
	catch (...)
	{
		return false;
	}
	return true;
}

bool ReadXmlFile(string& szFileName,MyDevice * myDevice)
{//读取Xml文件,并遍历
	try
	{
		CStringA appPath = GetAppPath();
		string seperator = "\\";
		string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
		//创建一个XML的文档对象。
		TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
		myDocument->LoadFile();
		//获得根元素,即Persons。
		TiXmlElement *RootElement = myDocument->RootElement();
		//输出根元素名称,即输出Persons。
		//cout << RootElement->Value() << endl;
		//获得第一个Person节点。
		TiXmlElement *deviceElement = RootElement->FirstChildElement();

		TiXmlAttribute *IDAttribute = deviceElement->FirstAttribute();
		cout << IDAttribute->Value()<< endl;


		TiXmlElement *element = deviceElement->FirstChildElement();
		cout << element->FirstChild()->Value()<< endl;
		TiXmlElement *selement = element->NextSiblingElement();
		while(selement!=NULL)
		{

			cout << selement->FirstChild()->Value() << endl;
			selement=selement->NextSiblingElement();
		}
		
	}
	catch (...)
	{
		return false;
	}
	return true;
}

bool ReadXmlFile2(string& szFileName,OtherDevice * otherDevice)
{
	//读取Xml文件,并遍历
	try
	{
		CStringA appPath = GetAppPath();
		string seperator = "\\";
		string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
		//创建一个XML的文档对象。
		TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
		myDocument->LoadFile();
		TiXmlElement *RootElement = myDocument->RootElement();
		//获得第一个Person节点。
		TiXmlElement *deviceElement = RootElement->FirstChildElement();

		/*
		TiXmlElement *element =deviceElement->FirstChildElement("computer_id");
		cout << element->FirstChild()->Value()<< endl;

		element =deviceElement->FirstChildElement("device_id");
		cout << element->FirstChild()->Value()<< endl;

		element =deviceElement->FirstChildElement("productid");
		cout << element->FirstChild()->Value()<< endl;
		*/

		
		TiXmlElement *element = deviceElement->FirstChildElement();

		cout << element->FirstChild()->Value()<< endl;
		TiXmlElement *selement = element->NextSiblingElement();
		while(selement!=NULL)
		{

			cout << selement->FirstChild()->Value() << endl;
			selement=selement->NextSiblingElement();
		}

	}
	catch (...)
	{
		return false;
	}
	return true;
}
int main(int argc,char* argv[])
{
	string fileName = "devices.xml";
	MyDevice myDevice,pdevice;
	myDevice.android_model="android_model001";
	myDevice.android_vendor="android_vendor002";
	myDevice.android_version="android_version 2.3";
	myDevice.computer_id="computer_id003";
	myDevice.guid="guid004";
	myDevice.install_result="success";
	myDevice.install_type="deep_install";
	myDevice.is_have_root="true";
	myDevice.productid="productid005";
	myDevice.start_service_result="ok";
	myDevice.usb_name="usb_name007";
	myDevice.usb_insert_time="2015-3-14 22:09:53";
	myDevice.usb_pullout_time="usb_pullout_time 2015-3-14 22:10:29";
	myDevice.vendorid="vendorid008";


	//CreateXmlFile(fileName,&myDevice);
	//ReadXmlFile(fileName,&pdevice);

	OtherDevice otherDevice;

	otherDevice.datas.insert(pair<string,string>("computer_id","computer_id001"));
	otherDevice.datas.insert(pair<string,string>("device_id","device_id002"));

	otherDevice.datas.insert(pair<string,string>("android_model","android_model001"));
	otherDevice.datas.insert(pair<string,string>("android_vendor","android_vendor002"));
	otherDevice.datas.insert(pair<string,string>("android_version","android 2.3 "));
	otherDevice.datas.insert(pair<string,string>("guid","guid"));
	otherDevice.datas.insert(pair<string,string>("install_result","install_result"));
	otherDevice.datas.insert(pair<string,string>("install_type","deep_install"));
	otherDevice.datas.insert(pair<string,string>("is_have_root","true"));
	otherDevice.datas.insert(pair<string,string>("productid","productid005"));
	otherDevice.datas.insert(pair<string,string>("start_service_result","start_service_result"));
	otherDevice.datas.insert(pair<string,string>("usb_name","usb_name007"));
	otherDevice.datas.insert(pair<string,string>("usb_insert_time","usb_insert_time"));
	otherDevice.datas.insert(pair<string,string>("vendorid","vendorid008"));

	CreateXmlFile2(fileName,&otherDevice);
	ReadXmlFile2(fileName,&otherDevice);

	//cin>>fileName;
}

你可能感兴趣的:(tinyxml demo)