tinyxml.h
tinyxmlerror.cpp
tinyxmlparser.cpp
tinyxml.cpp
tinystr.h
tinystr.cpp
.h
// 下列 ifdef 块是创建使从 DLL 导出更简单的
// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 XML_EXPORTS
// 符号编译的。在使用此 DLL 的
// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
// XML_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
// 符号视为是被导出的。
#pragma once
#ifdef XML_EXPORTS
#define XML_API __declspec(dllexport)
#else
#define XML_API __declspec(dllimport)
#endif
#include <list>
#define MAX_NUMBER 20
#deifne MAX_NAME 512
struct TData
{
int i_Id;
int i_Type;
char s_Name[MAX_NAME];
int i_TNum;
int TS[MAX_NUMBER]; // 45, 90, 135, ...
float s;
};
class XML_API CMTData
{
public:
CMTData(void);
~CMTData();
typedef std::list<TData> TData_List;
TData_List & getList()
{
return m_TDataList;
}
void addData(TData Data)
{
m_TDataList.push_back(Data);
}
private:
TData_List m_TDataList;
};
// 此类是从 XML.dll 导出的
class XML_API CT_XML {
public:
CT_XML(void);
// TODO: 在此添加您的方法。
// bUpdate::ture - 读取数据库(xml)更新TElem; flase - 用TElem更新数据库(xml)
void updateTdata(bool bUpdate, CMTData & TElem);
TCHAR s_WorkDir[1024];
TCHAR s_TData[128];
void TCHARTochar(char* dChar, TCHAR* str);
BOOL LoadData(CMTData & TElem);
void SaveData(CMTData & TElem);
};
.cpp
// XML.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "tinyxml.h"
#include "xml.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
}
}
else
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: GetModuleHandle 失败\n"));
nRetCode = 1;
}
return nRetCode;
}
CT_XML::CT_XML( void )
{
memset(s_WorkDir,0,sizeof(s_WorkDir));
memset(s_TData,0,sizeof(s_TData));
}
//
// bUpdate: ture - 读取数据库(xml)更新dbEnt;
// flase - 用dbEnt更新数据库(xml)
void CT_XML::updateDB( bool bUpdate, CMTData & TElem )
{
if (bUpdate)
{
LoadData(TElem);
}
else
{
SaveData(TElem);
}
}
void CT_XML::TCHARTochar( char* dChar, TCHAR* str )
{
int nMultiByteLen = WideCharToMultiByte(CP_ACP, 0, str, -1, dChar, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, str, -1, dChar, nMultiByteLen, NULL, NULL);
dChar[nMultiByteLen]=0;
}
BOOL CT_XML::LoadData(CMTData & TElem )
{
TCHAR DOTA[_MAX_PATH];
TCHAR LOL[_MAX_PATH];
TCHAR QQ[_MAX_PATH];
//将工作路径赋值
_tcscpy(gongzuolujin,s_WorkDir);
_tcscpy(LOL,L"\\Test11\\");
//路径相加(s_WorkDir\Test11\s_TData)
_tcscpy(DOTA,_tcscat(QQ,_tcscat(LOL,s_TData)));
//DOTA = "s_WorkDir\Test11\s_TData"
TCHARTochar(FilePath,DOTA);
TiXmlDocument doc(FilePath);
if (!doc.LoadFile())
return false;
TiXmlHandle hDoc(&doc);
TiXmlElement* pElem;
TiXmlHandle hRoot(0);
const char *tmpChar;
pElem=hDoc.FirstChildElement().Element();
if (!pElem)
return false;
tmpChar=pElem->Value();
hRoot=TiXmlHandle(pElem);
TiXmlElement* pLayer2 = hRoot.FirstChild( "2" ).Element();
if(!pLayer2)
return false;
DWORD dwID=0;
for(; pLayer2; pLayer2=pLayer2->NextSiblingElement())
{
const char* pName;
DB_Layer dblay;
TiXmlElement* pParam = pLayer2->FirstChild("3")->ToElement();
pName=pParam->Attribute("name");
TiXmlElement* pLayer3 = pParam->FirstChildElement("4");
if(pLayer3)
{
pLayer3->QueryIntAttribute("Type",&dblay.i_Type);
if (pName)
strcpy(dblay.s_Name,pName);
pLayer3->QueryIntAttribute("TNum",&dblay.i_NLayer);
for (int i = 0 ; i <dblay.i_NLayer;i++)
{
int i_Temp = i+1;
char s[3];
char *p;
p = itoa(i_Temp,s,10);
char strDegree[10] = "T";
strcat(strDegree,p);
pLayer3->QueryIntAttribute(strDegree,&dblay.Degrees[i]);
}
dblay.i_Id= ++dwID;
}
TElem.addLayer(dblay);
}
return true;
}
void CT_XML::SaveData( CMTData & TElem )
{
TiXmlDocument TiDoc;
TiXmlDeclaration * decl = new TiXmlDeclaration("1.0","gb2312","");
TiDoc.LinkEndChild( decl );
TiXmlElement * pLayer1 = new TiXmlElement( "1" );
TiDoc.LinkEndChild( pLayer1 );
TiXmlComment * comment = new TiXmlComment();
comment->SetValue(" Settings for Test " );
pLayer1->LinkEndChild( comment );
char Char[512];
TCHAR tChar[512];
CMMat_DB_Ents::DB_LAY_LIST& TData=
TElem.getLayList();
CMMat_DB_Ents::DB_LAY_LIST::iterator ite=
TData.begin();
while(ite!= TData.end())
{
TiXmlElement * pLayer2 = new TiXmlElement( "2" );
pLayer1->LinkEndChild(pLayer2);
TiXmlElement * shuo_ming = new TiXmlElement("这一层是干嘛滴");
pLayer2->LinkEndChild( shuo_ming );
TiXmlElement * pLayer3 = new TiXmlElement( "3" );
strcpy(Char,ite->s_Name);
pLayer3->SetAttribute("name", Char);
pLayer2->LinkEndChild( pLayer3 );
TiXmlElement * pLayer4 = new TiXmlElement( "4" );
pLayer4->SetAttribute("Type", ite->i_Type);
pLayer4->SetAttribute("TNum", ite->i_NLayer);
for (int i = 0 ; i < ite->i_NLayer;i++)
{
int i_Temp = i+1;
char s[3];
char *p;
p = itoa(i_Temp,s,10);
char strDegree[10] = "T";
strcat(strDegree,p);
pLayer4->SetAttribute(strDegree, ite->Degrees[i]);
}
pLayer3->LinkEndChild( pLayer4 );
ite++;
}
TCHAR DOTA[_MAX_PATH];
TCHAR LOL[_MAX_PATH];
TCHAR QQ[_MAX_PATH];
//将工作路径赋值
_tcscpy(gongzuolujin,s_WorkDir);
_tcscpy(LOL,L"\\Test11\\");
//路径相加(s_WorkDir\Test11\s_TData)
_tcscpy(DOTA,_tcscat(QQ,_tcscat(LOL,s_TData)));
//DOTA = "s_WorkDir\Test11\s_TData"
TCHARTochar(FilePath,DOTA);
TiDoc.SaveFile(FilePath);
}
example.xml
<?xml version="1.0" encoding="gb2312" ?>
<1>
<!-- Settings for Test -->
<2>
<这一层是干嘛滴 />
<3 name="测试1">
<4 Type="3" TNum="3" T1="45" T2="180" T3="90"/>
</3>
</2>
<2>
<这一层是干嘛滴 />
<3 name="测试2">
<4 Type="1" TNum="2" T1="90" T2="135"/>
</3>
</2>
<2>
<这一层是干嘛滴 />
<3 name="测试3">
<4 Type="2" TNum="2" T1="180" T2="45"/>
</3>
</2>
</1>