C++使用TinyXML

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

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

别人封装的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

.cpp文件

  1 #include <windows.h>
  2 #include <stdio.h>
  3 #include <tchar.h>
  4 #include "UDXml.h"
  5 #include <io.h>
  6 
  7 
  8 
  9 
 10 BOOL CUDXml::CreateXml(const char*  path,  const char* _version /*="1.0" */, const char* _encoding /*="UTF-8" */, const char* _standalone/*="no" */ )
 11 {
 12     m_doc.LoadFile(path, TIXML_ENCODING_UTF8);
 13     
 14     TiXmlNode *pDelar = new TiXmlDeclaration(_version, _encoding, _standalone);
 15     m_doc.InsertEndChild(*pDelar);delete pDelar;
 16     
 17     TiXmlNode *pRoot  = new TiXmlElement("root");
 18     m_doc.InsertEndChild(*pRoot);    
 19 
 20     return TRUE;
 21 }
 22 
 23 
 24 BOOL CUDXml::Open( const char* path )
 25 {
 26     return m_doc.LoadFile(path, TIXML_ENCODING_UTF8);
 27 }
 28 
 29 BOOL CUDXml::Save(const char* path)
 30 {
 31     BOOL ret;
 32 
 33     if(NULL == path) ret = m_doc.SaveFile();
 34     else ret = m_doc.SaveFile(path);
 35 
 36     return ret;
 37 }
 38 
 39 CUDXmlNodePtr  CUDXml::GetRoot()
 40 {
 41     return m_doc.RootElement();
 42 }
 43 
 44 BOOL CUDXml::IsExist( const char* path )
 45 {
 46     if(-1 != _access(path, 0))
 47         return TRUE;
 48     else
 49         return FALSE;
 50 }
 51 
 52 CUDXmlNodePtr CUDXmlNodePtr::NewChild( const char* name )
 53 {
 54     CUDXmlNodePtr NewNode;
 55     TiXmlNode *pNode = new TiXmlElement(name);
 56     
 57     NewNode = m_pElement->InsertEndChild(*pNode);
 58     
 59     delete pNode;
 60     return NewNode;
 61 }
 62 
 63 BOOL CUDXmlNodePtr::IsNull()
 64 {
 65     return NULL==m_pElement;
 66 }
 67 
 68 void CUDXmlNodePtr::SetAttribute( const char* name, const char* value )
 69 {
 70     m_pElement->SetAttribute(name, value);
 71 }
 72 
 73 std::string CUDXmlNodePtr::GetAttribute( const char* name )
 74 {    
 75     return     m_pElement->Attribute(name);
 76 }
 77 
 78 void CUDXmlNodePtr::SetName( const char*name )
 79 {
 80     m_pElement->SetValue(name);
 81 }
 82 
 83 std::string CUDXmlNodePtr::GetName()
 84 {
 85     const char *pName = m_pElement->Value();
 86     if(NULL == pName)
 87         return "";
 88     
 89     return pName;
 90 }    
 91 
 92 void CUDXmlNodePtr::SetValue( const char* value )
 93 {
 94     TiXmlNode *pText = new TiXmlText(value);
 95     m_pElement->InsertEndChild(*pText);
 96     delete pText;
 97 }
 98 
 99 std::string CUDXmlNodePtr::GetValue()
100 {
101     const char* pText = m_pElement->GetText();
102     if(NULL == pText)
103         return "";
104     
105     return pText;
106 }
107 
108 CUDXmlNodePtr CUDXmlNodePtr::GetFirstChild( const char* name/*=NULL*/ )
109 {
110     if(NULL == name)
111         return m_pElement->FirstChildElement();
112     else
113         return m_pElement->FirstChildElement(name);
114 }
115 
116 CUDXmlNodePtr CUDXmlNodePtr::NextSibling( const char* name/*=NULL*/ )
117 {
118     if(NULL == name)
119         return m_pElement->NextSiblingElement();
120     else
121         return m_pElement->NextSiblingElement(name); 
122 }
123 
124 CUDXmlNodePtr CUDXmlNodePtr::PreSibling( const char* name/*=NULL*/ )
125 {
126     if(NULL == name)
127         return m_pElement->PreviousSibling();
128     else
129         return m_pElement->PreviousSibling(name);
130 }
131 
132 BOOL CUDXmlNodePtr::Destory()
133 {
134     if(*this == Root())return FALSE;
135     
136     return    m_pElement->Parent()->RemoveChild((TiXmlNode*)m_pElement);
137 }
138 
139 CUDXmlNodePtr CUDXmlNodePtr::Root()
140 {
141     TiXmlElement *pElement = m_pElement;
142     TiXmlElement *pRoot = NULL;
143 
144     int nType = pElement->Type();
145     while (0 != nType)
146     {
147         pRoot    = pElement;
148         pElement = (TiXmlElement*)pElement->Parent();
149         nType    = pElement->Type();
150     }
151 
152     return pRoot;
153 }
154 
155 BOOL CUDXmlNodePtr::operator==( CUDXmlNodePtr&node )
156 {
157     return this->m_pElement == node.m_pElement;
158 }
159 
160 void CUDXmlNodePtr::DestoryAllChildren()
161 {
162     m_pElement->Clear();
163 }
164 
165 CUDXmlAttribute CUDXmlNodePtr::GetFirstAttribute()
166 {
167     return m_pElement->FirstAttribute();
168 }
169 
170 CUDXmlAttribute CUDXmlNodePtr::LastAttribute()
171 {
172     return m_pElement->LastAttribute();
173 }
174 
175 CUDXmlAttribute CUDXmlAttribute::Next()
176 {
177     return    m_pAttribute->Next();
178 }
179 
180 CUDXmlAttribute CUDXmlAttribute::Pre()
181 {
182     return m_pAttribute->Previous();
183 }
184 
185 std::string CUDXmlAttribute::Name()
186 {
187     return m_pAttribute->Name();
188 }
189 
190 std::string CUDXmlAttribute::Value()
191 {
192     return m_pAttribute->Value();
193 }
194 
195 void CUDXmlAttribute::SetName( const char* name )
196 {
197     m_pAttribute->SetName(name);
198 }
199 
200 void CUDXmlAttribute::SetValue( const char* value )
201 {
202     m_pAttribute->SetValue(value);
203 }
204 
205 double CUDXmlAttribute::DoubleValue()
206 {
207     return m_pAttribute->DoubleValue();
208 }
209 
210 int CUDXmlAttribute::IntValue()
211 {
212     return m_pAttribute->IntValue();
213 }
View Code

2014-11-1118:17:15

读取邮件FolderMap.xml自定义TinyXmlDll动态库

 1 #ifdef XMLDLL_EXPORTS  
 2 #define XMLDLL_API __declspec(dllexport)   
 3 #else  
 4 #define XMLDLL_API __declspec(dllimport)   
 5 #endif
 6 
 7 #include "xml\tinyxml.h"
 8 #include <windows.h>
 9 #include <stdio.h>
10 #include <tchar.h>
11 #include <string>
12 
13 using namespace std;
14 
15 namespace  ParserXml{
16 
17 class CPaserXml
18 {
19 public:
20 
21     /*获取节点值
22     *param domain_name 邮箱服务器域名
23     *param folder_name 文件夹名字
24     * 根据需求 return 文件夹名称(编码为UTF-8)
25     */
26     static XMLDLL_API std::string     GetValue(const char* domain_name,const char* folder_name,const char* filname="FolderMap.xml");
27 
28 private:
29     //              判断该文件是否存在
30     static XMLDLL_API BOOL            IsExist(const char* path);
31 
32     //              返回xml路径
33     static XMLDLL_API string          GetAppPath(const char* filename);
34 
35     //              宽字符转到Ansi
36     static XMLDLL_API std::string     WChar2Ansi(LPCWSTR pwszSrc);
37 
38     //              GB2312转到UTF-8
39     static XMLDLL_API std::string     GB2312ToUTF8(const char* gb2312);
40 
41     //              UTF-8转到GB2312
42     static XMLDLL_API std::string     UTF8ToGB2312(const char* utf8);
43 };
44 
45 }//namespace ParserXml
View Code
  1 #include "ParserXml.h"
  2 #include <io.h>
  3 #include <atlstr.h>
  4 
  5 namespace ParserXml{
  6 
  7 
  8 std::string CPaserXml::GetValue(const char* domain_name,const char* folder_name,const char* filname/*="FolderMap.xml"*/)
  9 {
 10     std::string NameValue;
 11 
 12     string xmlname = GetAppPath(filname);
 13 
 14     if (IsExist(xmlname.c_str()))
 15     {
 16         TiXmlDocument Document(xmlname.c_str());
 17 
 18         if(Document.LoadFile())
 19         {
 20             TiXmlElement* root = Document.RootElement(); 
 21             if (root)
 22             {
 23                 TiXmlNode* item = root->FirstChild(domain_name);
 24 
 25                 if (item)
 26                 {
 27                     TiXmlNode* child = item->FirstChild(GB2312ToUTF8(folder_name).c_str());
 28 
 29                     TiXmlElement* IDElement = child->ToElement()->FirstChildElement();
 30 
 31                     TiXmlElement* NameElement = IDElement->NextSiblingElement();
 32 
 33                     const char* name = NameElement->GetText();  
 34 
 35                     NameValue = name;
 36                 }
 37             }
 38         }
 39     }
 40 
 41     return NameValue;
 42 }
 43 
 44 BOOL CPaserXml::IsExist(const char* path)
 45 {
 46     if(-1 != _access(path, 0))
 47         return TRUE;
 48     else
 49         return FALSE;
 50 }
 51 
 52 
 53 string CPaserXml::GetAppPath(const char* filename)
 54 {
 55     //获取应用程序根目录
 56         
 57     TCHAR modulePath[MAX_PATH];
 58     GetModuleFileName(NULL, modulePath, MAX_PATH);
 59     CString strModulePath(modulePath);
 60     strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
 61 
 62     string strPath= WChar2Ansi(strModulePath.GetBuffer());
 63 
 64     string seperator = "\\";
 65 
 66     string fullPath = strPath +seperator+filename;
 67 
 68     return fullPath;
 69 }
 70 
 71 std::string CPaserXml::WChar2Ansi(LPCWSTR pwszSrc)
 72 {
 73     int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
 74     if (nLen<= 0) return std::string("");
 75     char* pszDst = new char[nLen];
 76     if (NULL == pszDst) return std::string("");
 77     WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
 78     pszDst[nLen -1] = 0;
 79     std::string strTemp(pszDst);
 80     delete [] pszDst;
 81     return strTemp;
 82 }
 83 
 84 std::string CPaserXml::GB2312ToUTF8(const char* gb2312)
 85 {
 86     int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
 87 
 88     wchar_t* wstr = new wchar_t[len+1];
 89 
 90     memset(wstr, 0, len+1);
 91 
 92     MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
 93 
 94     len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
 95 
 96     char* str = new char[len+1];
 97 
 98     memset(str, 0, len+1);
 99 
100     WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
101 
102     if(wstr) delete[] wstr;
103 
104     string temp = str;
105     return temp;
106 }
107 
108 std::string CPaserXml::UTF8ToGB2312(const char* utf8)
109 {
110     int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
111 
112     wchar_t* wstr = new wchar_t[len+1];
113 
114     memset(wstr, 0, len+1);
115 
116     MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
117 
118     len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
119 
120     char* str = new char[len+1];
121 
122     memset(str, 0, len+1);
123 
124     WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
125     if(wstr) delete[] wstr;
126 
127     string temp = str;
128     return temp;
129 }
130 
131 }//namespace ParserXml
View Code

 

转载于:https://www.cnblogs.com/chechen/p/4087716.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值