参照一:http://qaohao.iteye.com/blog/496237

参照二:http://hi.baidu.com/lnylvoeegzcgnrr/item/af68fd9cde40fc1a924f41f5

C++使用TinyXML

别人封装的TinyXML:

.h文件

  1 #include "xml\tinyxml.h"
  2 #include <string>
  3 using namespace std;
  4 
  5 #if !defined(AFX_STDAFX_H__UDXML_EF91_4EF1_A2F2_53AD89B23C30__INCLUDED_)
  6 #define AFX_STDAFX_H__UDXML_EF91_4EF1_A2F2_53AD89B23C30__INCLUDED_
  7 
  8 #if _MSC_VER > 1000
  9 #pragma once
 10 #endif 
 11 
 12 /******************************************************************************
 13   描述: 对tinyxml库封装的简单类: 
 14         1,创建、保存xml
 15         2,动态创建节点,更改名称以及值;动态删除
 16         3,动态增加、删除属性,遍历、读取属性名称以及值
 17 
 18   背景: tinyxml对xml操作的接口过于分散,为了适应自己的需要,对常用的接口进行简单的封
 19         装,各个接口增加注释,方便使用。
 20   
 21   环境: WinXP+VC6
 22   修改: 仇军利        EMAIL:282881515@163.COM        QQ:282881515
 23   版本: 2012-12-28    V1.0
 24   发布: CSDN
 25 ******************************************************************************/
 26 class CUDXmlAttribute
 27 {
 28 public:
 29     CUDXmlAttribute()                           { m_pAttribute=NULL      ; }
 30     CUDXmlAttribute( TiXmlAttribute *pAttribute){ m_pAttribute=pAttribute; }
 31 
 32     //              下一个属性
 33     CUDXmlAttribute Next();
 34     //              前一个属性
 35     CUDXmlAttribute Pre();
 36 
 37     //              返回属性名称
 38     string          Name();
 39     //              设置属性名称
 40     void            SetName(const char* name);
 41 
 42     //              返回属性值string类型
 43     string          Value();
 44     //              返回属性值int类型
 45     int             IntValue();
 46     //              返回属性值double类型
 47     double          DoubleValue();
 48     //              设置属性值
 49     void            SetValue(const char* value);  
 50 
 51 private:
 52     TiXmlAttribute *m_pAttribute;
 53 };
 54 
 55 class CUDXmlNodePtr
 56 {
 57 public:
 58     CUDXmlNodePtr()                         { m_pElement=NULL; }
 59     CUDXmlNodePtr(TiXmlElement *pElement)   { m_pElement=pElement; }
 60     CUDXmlNodePtr(TiXmlNode* pNode)         { m_pElement=pNode->ToElement(); }
 61     BOOL          operator==(CUDXmlNodePtr&node);
 62 
 63     //              添加新的节点
 64     CUDXmlNodePtr   NewChild(const char* name);
 65     //              获取第一个孩子节点,默认返回第一个孩子节点
 66     CUDXmlNodePtr   GetFirstChild(const char* name=NULL);
 67     //              获取下一个兄弟节点,默认返回下边第一个兄弟节点
 68     CUDXmlNodePtr   NextSibling(const char* name=NULL);
 69     //              获取上一个兄弟节点,默认返回上边第一个兄弟节点
 70     CUDXmlNodePtr   PreSibling(const char* name=NULL);
 71     //              自我销毁
 72     BOOL            Destory();
 73     //              销毁所有孩子节点
 74     void            DestoryAllChildren();
 75 
 76     //              设置属性
 77     void            SetAttribute(const char* name, const char* value);
 78     //              读取属性值
 79     string          GetAttribute(const char* name);
 80     CUDXmlAttribute GetFirstAttribute();
 81     CUDXmlAttribute LastAttribute();
 82 
 83     //              设置节点名称
 84     void            SetName(const char*name);
 85     //              获取节点名称
 86     string          GetName();
 87 
 88     //              设置节点值
 89     void            SetValue(const char* value);
 90     //              获取节点值
 91     string          GetValue();
 92 
 93     //              判断该节点是否为空
 94     BOOL            IsNull();
 95     //              返回根节点
 96     CUDXmlNodePtr   Root();
 97 
 98 public:
 99     TiXmlElement *m_pElement;
100 };
101 
102 class CUDXml
103 {
104 public:
105     //    创建xml文件 默认声明为<?xml version="1.0" encoding="UTF-8" standalone="no"?>
106     BOOL  CreateXml(const char* path, const char* version="1.0", const char*  encoding="UTF-8", const char*  standalone="no");
107     
108     //    打开文件
109     BOOL  Open(const char* path);
110 
111     //    保存文件
112     BOOL  Save(const char* path=NULL);
113 
114     //    获取根节点
115     CUDXmlNodePtr  GetRoot();
116 
117     //    判断该文件是否存在
118     BOOL  IsExist(const char* path);
119 
120 private:
121 
122     TiXmlDocument m_doc;
123 };
124 #endif 
View Code

相关文章:

  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2022-02-12
  • 2021-07-15
  • 2021-09-12
猜你喜欢
  • 2021-12-19
  • 2021-07-23
  • 2021-07-28
  • 2022-12-23
  • 2022-01-09
  • 2021-07-18
  • 2022-02-26
相关资源
相似解决方案