zoukankan      html  css  js  c++  java
  • Tinyxml封装类COperatorXml

    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'');
    	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, '');
    	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, '');
    	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();


     

  • 相关阅读:
    互斥锁的通俗理解
    U-Boot下分区信息查看
    《计算机组成原理》唐朔飞第二版_笔记
    《大话程序员》安晓辉_笔记
    C++ 类对象的初始化顺序
    FilterTerminal使用说明全总结
    sed -i 命令常用方法总结
    入园记录
    cookies,sessionStorage 和 localStorage区别
    优雅降级和渐进增强的理解:
  • 原文地址:https://www.cnblogs.com/dongc/p/5225121.html
Copyright © 2011-2022 走看看