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源文件


使用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();


 

相关文章:

  • 2021-09-28
  • 2021-11-16
  • 2021-05-23
  • 2021-07-05
  • 2021-09-28
  • 2021-05-18
猜你喜欢
  • 2021-12-08
  • 2021-06-04
  • 2022-12-23
  • 2022-01-28
  • 2021-10-20
  • 2021-05-09
  • 2021-07-03
相关资源
相似解决方案