OperatorXml.h頭文件
#ifndef _OPERATOR_XML_H_
#define _OPERATOR_XML_H_
#include <string>
class TiXmlDocument;
class TiXmlElement;
class TiXmlDeclaration;
class COperaotrXml
{
public:
//////////////////////////////////////////////////////////////////////////
// 操作類型:
// OperatorTypeNone,無類型
// OperatorTypeFirstChild,第一個子元素
// OperatorTypeNextChild,下一個子元素
//////////////////////////////////////////////////////////////////////////
enum OperatorType
{
OperatorTypeNone = 0,
OperatorTypeFirstChild,
OperatorTypeNextChild,
};
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// UTF-8轉Unicode
//////////////////////////////////////////////////////////////////////////
static bool UTF8ToUnicode(const std::string& strUTF8, std::wstring* pStrUnicode);
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// Unicode轉UTF-8
//////////////////////////////////////////////////////////////////////////
static bool UnicodeToUTF8(const std::wstring& strUnicode, std::string* pStrUTF8);
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// Unicode轉ASCII
//////////////////////////////////////////////////////////////////////////
static bool UnicodeToASCII(const std::wstring& strUnicode, std::string* pStrASCII);
public:
COperaotrXml();
~COperaotrXml();
public:
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 設置聲明
// 輸入參數:
// strVersion,版本("1.0")
// strEncoding,編碼("UTF-8")
// strStandalone,是否獨立("yes"或"no")
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool SetDeclaration(const std::wstring& strVersion,
const std::wstring& strEncoding,
const std::wstring& strStandalone);
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 獲取聲明
//////////////////////////////////////////////////////////////////////////
TiXmlDeclaration* GetDeclaration();
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 添加元素
// 輸入參數:
// strName,元素標籤名
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool AddElement(const std::wstring& strName);
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 添加元素
// 輸入參數:
// strName,元素標籤名
// strText,元素內容
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool AddElement(const std::wstring& strName, const std::wstring& strText);
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 查找元素
// 輸入參數:
// strName,元素標籤名
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool FindElement(const std::wstring& strName) const;
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 得到元素內容
//////////////////////////////////////////////////////////////////////////
std::wstring GetText() const;
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 加載文件
//////////////////////////////////////////////////////////////////////////
bool Load(const std::wstring& strFileName);
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 保存爲文件
//////////////////////////////////////////////////////////////////////////
bool Save(const std::wstring& strFileName);
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 進入某個元素
//////////////////////////////////////////////////////////////////////////
void Into();
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 跳出某個元素
//////////////////////////////////////////////////////////////////////////
void Out();
private:
//禁止拷貝操作
COperaotrXml(const COperaotrXml&);
//禁止賦值操作
COperaotrXml& operator =(const COperaotrXml&);
private:
mutable TiXmlElement* m_pCurrentElement;//當前元素
mutable TiXmlElement* m_pChildElement;//子元素
TiXmlDocument* m_pXmlDoc;//xml文檔
mutable OperatorType m_emOperatorType;//操作類型
};
#endif
OperatorXml.cpp源文件
#include "OperatorXml.h"
#include "../../Tinyxml/Tinyxml/tinyxml.h"
#include <windows.h>
//////////////////////////////////////////////////////////////////////////
// 由於DLL導出函數接口中使用了非內置類型(如std::string等等),必須使用相同版本
// 的lib和dll庫,否則會出現莫名其妙的錯誤.
//////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#pragma comment(lib, "../Debug/tinyxml.lib")
#else
#pragma comment(lib, "../Release/tinyxml.lib")
#endif
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// UTF-8轉Unicode
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::UTF8ToUnicode(const std::string& strUTF8, std::wstring* pStrUnicode)
{
if (strUTF8.empty() || !pStrUnicode)
{
return false;
}
int nLength = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);
int nWideLength = nLength - 1;
pStrUnicode->resize(nWideLength, L'\0');
MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, &pStrUnicode->at(0), nWideLength);
return true;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// Unicode轉UTF-8
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::UnicodeToUTF8(const std::wstring& strUnicode, std::string* pStrUTF8)
{
if (strUnicode.empty() || !pStrUTF8)
{
return false;
}
int nLength = WideCharToMultiByte(CP_UTF8, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
pStrUTF8->resize(nLength - 1, '\0');
WideCharToMultiByte(CP_UTF8, 0, strUnicode.c_str(), -1, &pStrUTF8->at(0), nLength - 1, NULL, NULL);
return true;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// Unicode轉ASCII
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::UnicodeToASCII(const std::wstring& strUnicode, std::string* pStrASCII)
{
if (strUnicode.empty() || !pStrASCII)
{
return false;
}
int nLength = WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
pStrASCII->resize(nLength - 1, '\0');
WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, &pStrASCII->at(0), nLength - 1, NULL, NULL);
return true;
}
COperaotrXml::COperaotrXml()
: m_pCurrentElement(NULL),
m_pChildElement(NULL),
m_pXmlDoc(NULL)
{
m_pXmlDoc = new TiXmlDocument;
}
COperaotrXml::~COperaotrXml()
{
delete m_pXmlDoc;
m_pXmlDoc = NULL;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 設置聲明
// 輸入參數:
// pszVersion,版本
// pszEncoding,編碼
// pszStandalone,是否獨立("yes"或"no")
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::SetDeclaration(const std::wstring& strVersion,
const std::wstring& strEncoding,
const std::wstring& strStandalone)
{
bool bRet = false;
try
{
if (!m_pXmlDoc)
{
bRet = false;
throw bRet;
}
TiXmlNode* pNode = NULL;
TiXmlDeclaration* pDecl = NULL;
//獲取聲明
pNode = m_pXmlDoc->FirstChild();
if (pNode)
{
pDecl = pNode->ToDeclaration();
}
std::string _version;
std::string _encoding;
std::string _standalone;
//編碼轉換
UnicodeToUTF8(strVersion, &_version);
UnicodeToUTF8(strEncoding, &_encoding);
UnicodeToUTF8(strStandalone, &_standalone);
//設置聲明
TiXmlDeclaration decl(_version, _encoding,_standalone);
if (pDecl)
{
*pDecl = decl;
}
else
{
if (pNode)
{
m_pXmlDoc->InsertBeforeChild(pNode, decl);
}
else
{
pDecl = new TiXmlDeclaration(decl);
m_pXmlDoc->LinkEndChild(pDecl);
}
}
bRet = true;
}
catch (bool)
{
}
return bRet;
}
TiXmlDeclaration* COperaotrXml::GetDeclaration()
{
TiXmlDeclaration* pDecl = NULL;
if (m_pXmlDoc)
{
TiXmlElement* pRoot = m_pXmlDoc->RootElement();
if (pRoot)
{
pDecl = pRoot->ToDeclaration();
}
}
return pDecl;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 添加元素
// 輸入參數:
// strName,元素標籤名
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::AddElement(const std::wstring& strName)
{
bool bRet = false;
try
{
//xml節點元素標籤名稱必須以字母開頭
if (strName.empty() || !m_pXmlDoc || !isalpha(strName[0]))
{
bRet = false;
throw bRet;
}
std::string strUTF8;
TiXmlElement* pElem = NULL;
//添加元素
UnicodeToUTF8(strName, &strUTF8);
pElem = new TiXmlElement(strUTF8);
if (m_pCurrentElement)
{
m_pCurrentElement->LinkEndChild(pElem);
}
else if (m_pXmlDoc)
{
m_pXmlDoc->LinkEndChild(pElem);
}
else
{
bRet = false;
throw bRet;
}
m_pChildElement = pElem;
m_emOperatorType = OperatorTypeNextChild;
bRet = true;
}
catch (bool)
{
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 添加元素
// 輸入參數:
// strName,元素標籤名
// strText,元素內容
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::AddElement(const std::wstring& strName, const std::wstring& strText)
{
bool bRet = AddElement(strName);
if (bRet)
{
std::string strUTF8;
TiXmlText* pText = NULL;
//添加元素的內容
UnicodeToUTF8(strText, &strUTF8);
pText = new TiXmlText(strUTF8);
if (m_pChildElement)
{
m_pChildElement->LinkEndChild(pText);
}
else
bRet = false;
m_emOperatorType = OperatorTypeNextChild;
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 查找元素
// 輸入參數:
// strName,元素標籤名
// 返回值:
// true,成功;false,失敗
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::FindElement(const std::wstring& strName) const
{
bool bRet = false;
try
{
//xml節點元素標籤名稱必須以字母開頭
if (strName.empty() || !m_pXmlDoc || !isalpha(strName[0]))
{
bRet = false;
throw bRet;
}
std::string strUTF8;
UnicodeToUTF8(strName, &strUTF8);
//查找第一個子元素
if (m_emOperatorType == OperatorTypeFirstChild)
{
if (m_pCurrentElement)
{
m_pChildElement = m_pCurrentElement->FirstChildElement(strUTF8);
}
else
{
m_pChildElement = m_pXmlDoc->FirstChildElement(strUTF8);
}
if (m_pChildElement)
{
bRet = true;
m_emOperatorType = OperatorTypeNextChild;
}
}
//查找下一個子元素
else if (m_emOperatorType == OperatorTypeNextChild)
{
if (m_pChildElement)
{
m_pChildElement = m_pChildElement->NextSiblingElement(strUTF8);
}
if (m_pChildElement)
{
bRet = true;
}
}
}
catch (bool)
{
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 得到元素內容
//////////////////////////////////////////////////////////////////////////
std::wstring COperaotrXml::GetText() const
{
std::wstring strText;
const char* pszText = NULL;
if (m_pChildElement)
{
pszText = m_pChildElement->GetText();
if (pszText)
{
UTF8ToUnicode(pszText, &strText);//編碼轉換
}
}
return strText;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 加載文件
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::Load(const std::wstring& strFileName)
{
bool bRet = false;
try
{
if (strFileName.empty())
{
bRet = false;
throw bRet;
}
std::string name;
UnicodeToUTF8(strFileName, &name);
if (m_pXmlDoc)
{
delete m_pXmlDoc;
m_pXmlDoc = NULL;
}
m_pXmlDoc = new TiXmlDocument(name);
bRet = m_pXmlDoc->LoadFile();
m_emOperatorType = OperatorTypeFirstChild;
m_pCurrentElement = NULL;
m_pChildElement = NULL;
}
catch (bool)
{
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 保存爲文件
//////////////////////////////////////////////////////////////////////////
bool COperaotrXml::Save(const std::wstring& strFileName)
{
bool bRet = false;
try
{
if (strFileName.empty() || !m_pXmlDoc)
{
bRet = false;
throw bRet;
}
std::string name;
TiXmlDeclaration* pDecl = GetDeclaration();
if (!pDecl)
{
SetDeclaration(L"1.0", L"UTF-8", L"");
}
UnicodeToUTF8(strFileName, &name);
bRet = m_pXmlDoc->SaveFile(name);
}
catch (bool)
{
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 進入某個元素
//////////////////////////////////////////////////////////////////////////
void COperaotrXml::Into()
{
m_pCurrentElement = m_pChildElement;
m_pChildElement = NULL;
m_emOperatorType = OperatorTypeFirstChild;
}
//////////////////////////////////////////////////////////////////////////
// 函數功能:
// 跳出某個元素
//////////////////////////////////////////////////////////////////////////
void COperaotrXml::Out()
{
if (m_pCurrentElement)
{
TiXmlNode* pNode = m_pCurrentElement->Parent();
m_pChildElement = m_pCurrentElement;
if (pNode)
{
m_pCurrentElement = pNode->ToElement();
}
else
{
m_pCurrentElement = NULL;
}
m_emOperatorType = OperatorTypeNextChild;
}
}
使用COperatorXml
COperaotrXml xml;
std::wstring name;
std::wstring val;
xml.SetDeclaration(L"1.0", L"UTF-8", L"");
name = L"T1";
xml.AddElement(name);
xml.Into();
name = L"T1-1";
xml.AddElement(name);
name = L"T1-2";
xml.AddElement(name);
name = L"T1-3";
xml.AddElement(name);
name = L"T1-4";
xml.AddElement(name);
name = L"T1-5";
xml.AddElement(name);
xml.Into();
name = L"T1-5-1";
val = L"1";
xml.AddElement(name, val);
name = L"T1-5-2";
val = L"2";
xml.AddElement(name, val);
name = L"T1-5-3";
val = L"3";
xml.AddElement(name, val);
name = L"T1-5-4";
val = L"4";
xml.AddElement(name, val);
name = L"T1-5-5";
val = L"5";
xml.AddElement(name, val);
xml.Out();
name = L"T1-6";
xml.AddElement(name);
name = L"T1-7";
xml.AddElement(name);
xml.Out();
name = L"T2";
xml.AddElement(name);
xml.Into();
name = L"T2-1";
xml.AddElement(name);
name = L"T2-2";
xml.AddElement(name);
name = L"T2-3";
xml.AddElement(name);
name = L"T2-4";
xml.AddElement(name);
name = L"T2-5";
xml.AddElement(name);
xml.Out();
name = L"T3";
xml.AddElement(name);
name = L"T4";
xml.AddElement(name);
name = L"T5";
xml.AddElement(name);
xml.Into();
name = L"T5-1";
xml.AddElement(name);
name = L"T5-2";
xml.AddElement(name);
name = L"T5-3";
xml.AddElement(name);
name = L"T5-4";
xml.AddElement(name);
name = L"T5-5";
xml.AddElement(name);
xml.Out();
name = L"T6";
xml.AddElement(name);
bool bRet = false;
xml.Save(L"3.xml");
xml.Load(L"3.xml");
name = L"T1";
bRet = xml.FindElement(name);
xml.Into();
name = L"T1-1";
bRet = xml.FindElement(name);
name = L"T1-5";
bRet = xml.FindElement(name);
xml.Into();
name = L"T1-5-1";
bRet = xml.FindElement(name);
val = xml.GetText();
xml.Out();
xml.Out();
name = L"T2";
bRet = xml.FindElement(name);
xml.Into();
name = L"T2-1";
bRet = xml.FindElement(name);
name = L"T2-5";
bRet = xml.FindElement(name);
xml.Out();
name = L"T3";
bRet = xml.FindElement(name);
name = L"T5";
bRet = xml.FindElement(name);
xml.Into();
name = L"T5-1";
bRet = xml.FindElement(name);
name = L"T5-2";
bRet = xml.FindElement(name);
name = L"T5-3";
bRet = xml.FindElement(name);
xml.Into();
name = L"abc";
val = L"abc";
xml.AddElement(name, val);
xml.Out();
xml.Out();