使用tinyxml读xml文件信息到结构体

下载TinyXML的网址:http://www.grinninglizard.com/tinyxml/

  使用TinyXML只需要将其中的6个文件拷贝到项目中就可以直接使用了,这六个文件是:tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。

 

  1 // xmldemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2 //
  3 
  4 #include "pch.h"
  5 #include 
  6 #include <string>
  7 #include "tinyxml.h"
  8 #include "tinystr.h"
  9 #include 
 10 
 11 #include  
 12 #define N 5
 13 
 14 using namespace std;
 15 
 16 struct Stu
 17 {
 18     string name;
 19     int age;
 20     string gender;
 21 }student[N];
 22 int count_student = 0;
 23 
 24 #define U   (CUtf8String)  
 25 #define W   (CGb2312String)
 26 class CUtf8String
 27 {
 28 public:
 29     inline CUtf8String(const char* gb2312)
 30     {
 31         m_bIsConst = true;
 32 #ifdef TEST_TYPE_MAP  
 33         if (m[gb2312])
 34         {
 35             m_utf8 = m[gb2312];
 36             return;
 37         }
 38 #endif  
 39         int buffLen = 0;
 40         WCHAR wbuff[5120];
 41         MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
 42         buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 0, 0, 0);
 43         m_utf8 = new char[buffLen + 1];
 44         WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 0, 0);
 45 #ifdef TEST_TYPE_MAP  
 46         m[gb2312] = m_utf8;
 47 #endif  
 48     }
 49 
 50     inline CUtf8String(char* gb2312)
 51     {
 52         m_bIsConst = false;
 53         int buffLen = 0;
 54         WCHAR wbuff[5120];
 55         MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wbuff, 5120);
 56         buffLen = WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, NULL, 0, 0, 0);
 57         m_utf8 = new char[buffLen + 1];
 58         WideCharToMultiByte(CP_UTF8, 0, wbuff, -1, (LPSTR)m_utf8, buffLen, 0, 0);
 59     }
 60 
 61     inline ~CUtf8String()
 62     {
 63 #ifndef TEST_TYPE_MAP  
 64         if (m_utf8)
 65         {
 66             delete m_utf8;
 67             m_utf8 = 0;
 68         }
 69 #else  
 70         if (!m_bIsConst)
 71         {
 72             if (m_utf8)
 73             {
 74                 delete m_utf8;
 75                 m_utf8 = 0;
 76             }
 77         }
 78 #endif  
 79     }
 80 
 81     inline operator char*()
 82     {
 83         return (char*)m_utf8;
 84     }
 85 private:
 86     const char* m_utf8;
 87     bool m_bIsConst;
 88 #ifdef TEST_TYPE_MAP  
 89     static strmap m;
 90 #endif  
 91 };
 92 
 93 class CGb2312String
 94 {
 95 public:
 96     inline CGb2312String(const char* utf8)
 97     {
 98 #ifdef TEST_TYPE_MAP  
 99         if (m[utf8])
100         {
101             m_gb2312 = 0;
102             m_gb2312 = m[utf8];
103         }
104 #endif  
105         int buffLen = 0;
106         WCHAR wbuff[5120];
107         MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
108         buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 0, 0, 0);
109         m_gb2312 = new char[buffLen + 1];
110         WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 0, 0);
111 #ifdef TEST_TYPE_MAP  
112         m[utf8] = m_gb2312;
113 #endif  
114     }
115 
116     inline CGb2312String(char* utf8)
117     {
118 #ifdef TEST_TYPE_MAP  
119         if (m[utf8])
120         {
121             m_gb2312 = 0;
122             m_gb2312 = m[utf8];
123         }
124 #endif  
125         int buffLen = 0;
126         WCHAR wbuff[5120];
127         MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuff, 5120);
128         buffLen = WideCharToMultiByte(CP_ACP, 0, wbuff, -1, NULL, 0, 0, 0);
129         m_gb2312 = new char[buffLen + 1];
130         WideCharToMultiByte(CP_ACP, 0, wbuff, -1, (LPSTR)m_gb2312, buffLen, 0, 0);
131 #ifdef TEST_TYPE_MAP  
132         m[utf8] = m_gb2312;
133 #endif  
134     }
135 
136     inline ~CGb2312String()
137     {
138 #ifndef TEST_TYPE_MAP  
139         if (m_gb2312)
140         {
141             delete m_gb2312;
142             m_gb2312 = 0;
143         }
144 #endif  
145     }
146 
147     inline operator char*()
148     {
149         return (char*)m_gb2312;
150     }
151 private:
152     const char* m_gb2312;
153 #ifdef TEST_TYPE_MAP  
154     static strmap m;
155 #endif  
156 };
157 
158 
159 void create_testXml()
160 {
161     string fileName = "test.xml";
162     TiXmlDocument *doc = new TiXmlDocument();                //创建xml文档对象
163 
164     TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0", "UTF-8", "");
165     doc->LinkEndChild(pDeclaration);
166 
167     TiXmlElement *RootLv1 = new TiXmlElement("Persons");      //创建一个根结点
168     doc->LinkEndChild(RootLv1);                                //链接到文档对象下
169 
170     TiXmlElement *RootLv2 = new TiXmlElement("Person");        //创建一个节点
171     RootLv1->LinkEndChild(RootLv2);                            //链接到节点RootLv1下
172     RootLv2->SetAttribute("Number", "1");                    //设置节点RootLv2属性
173 
174     TiXmlElement *Name = new TiXmlElement("name");            //创建节点
175     RootLv2->LinkEndChild(Name);                            //链接节点到RootLv2下
176 
177     TiXmlElement *Price = new TiXmlElement("price");        //创建节点
178     RootLv2->LinkEndChild(Price);                            //链接节点到RootLv2下
179 
180     TiXmlText *NameText = new TiXmlText("Robin");            //创建XmlText文本
181     Name->LinkEndChild(NameText);                            //链接到Name下
182 
183     TiXmlText *PriceText = new TiXmlText("22.5");            //创建XmlText
184     Price->LinkEndChild(PriceText);                            //链接到Price下
185 
186     doc->SaveFile("d:\\temp\\test.xml");                            //保存到文件
187 }
188 
189 enum SuccessEnum { FAILURE, SUCCESS };
190 SuccessEnum loadXML()
191 {
192     TiXmlDocument *doc = new TiXmlDocument();
193     string m_strXmlPath = "d:\\temp\\person.xml";
194     cout << m_strXmlPath << endl;
195     if (!doc->LoadFile(m_strXmlPath.c_str()))
196     {
197         cout<<"无法加载xml文件!" << endl;
198         return FAILURE;
199     }
200 
201     TiXmlElement* root = doc->FirstChildElement();
202     if (root == NULL)
203     {
204         cerr << "Failed to load file: No root element." << endl;
205         doc->Clear();
206         return FAILURE;
207     }
208 
209     int i = 0;
210     //读到Persons
211     for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
212     {
213         /*string elemName = elem->Value();
214         const char* attr;
215         attr = elem->Attribute("priority");
216         if (strcmp(attr, "1") == 0)
217         {
218             TiXmlElement* e1 = elem->FirstChildElement("bold");
219             TiXmlNode* e2 = e1->FirstChild();
220             cout << "priority=1\t" << e2->ToText()->Value() << endl;
221 
222         }
223         else if (strcmp(attr, "2") == 0)
224         {
225             TiXmlNode* e1 = elem->FirstChild();
226             cout << "priority=2\t" << e1->ToText()->Value() << endl;
227         }*/
228     
229         //索引到Person节点,分别读取3个数值
230         TiXmlElement* name = elem->FirstChildElement();
231         string S_name = (string)W(name->GetText());
232         /*TiXmlNode* name_value = name->FirstChild();
233         string S_name = name_value->ToText()->Value();*/
234 
235         TiXmlElement* age = name->NextSiblingElement();
236         int S_age = (int)atof(age->GetText());
237         /*TiXmlNode* age_value = age->FirstChild();
238         int S_age = (int)atof(age_value->ToText()->Value());*/
239 
240         TiXmlElement* gender = age->NextSiblingElement();
241         string S_gender = (string)W(gender->GetText());
242         /*TiXmlNode* gender_value = gender->FirstChild();
243         string S_gender = gender_value->ToText()->Value();*/
244         cout << "name:" << S_name << " \tage:" << S_age << " \tgender:" << S_gender << endl;
245 
246         if (i < N)
247         {
248             student[i].name = S_name;
249             student[i].age = S_age;
250             student[i].gender = S_gender;
251         }
252         i++;
253                 
254     }
255     count_student = i;
256     //cout << "count_student=" << count_student << endl;
257     
258     doc->Clear();
259     return SUCCESS;
260 }
261 
262 void sortStu()
263 {
264     int i, j;
265     for (i = 0; i < count_student -1; i++)
266         for (j = 0; j < count_student - i - 1; j++)
267         {
268             if (student[j].age > student[j + 1].age)
269             {
270                 struct Stu temp = student[j];
271                 student[j] = student[j + 1];
272                 student[j + 1] = temp;
273             }
274         }
275     
276 }
277 
278 void write2txt()
279 {
280     FILE *fp;
281     if ((fp = fopen("D:\\temp\\person.txt", "w+")) == NULL) {
282         printf("Cannot open file, press any key exit!");
283         //getch();
284         exit(1);
285     }
286     for (int i = 0; i < count_student; i++) {
287         fprintf(fp, "%s %d %s\n", student[i].name.c_str(), student[i].age, student[i].gender.c_str());
288         //cout << student[i].name.c_str() << student[i].age << student[i].gender.c_str() << endl;
289     }
290     fclose(fp);
291 }
292 
293 int main(int argc, char* argv[])
294 {
295     
296     if (loadXML() == FAILURE)
297     {
298         return 1;
299     }
300     sortStu();
301     write2txt();
302     return 0;
303 }

 

你可能感兴趣的:(使用tinyxml读xml文件信息到结构体)