C++之tinyXML的使用详解

tinyXML一款很优秀的操作C++类库,文件不大,但方法很丰富,和apache的Dom4j可以披靡啊!习惯了使用java类库的我看到这么丰富的c++类库,很高兴!它使用很简单,只需要拷贝几个文件到你的工程中,没有STL也可以编译运行。 
    
    下面我从这几个方面谈谈对tinyXML类库的使用以及理解。 

    首先在sourceforge上下载tinyXML类库,地址:http://sourceforge.net/projects/tinyxml/ 

    然后解压缩tinyXML后,将这六个文件添加到你的c++工程中,分别是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在需要操作xml文件的地方,使用如下代码,就可以引入tinyXML类库。

  1. C++代码  
    #include<tinyxml>  
    
    或者
    C++代码  
    #include "tinyxml.h"  

下面我用个简单的例子说明如何使用tinyXML操作xml文件。在讲例子之前我先说说tinyXML中主要类和xml文档之间的对应关系。下面是tinyXML中主要class的类图,反应各个类之间的静态关系。 

引用来自tinyXML文档

       TiXmlBase是所有类的基类,TiXmlNode、TiXmlAttribute两个类都继承来自TiXmlBase类,其中TiXmlNode类指的是所有被<...>...<.../>包括的内容,而xml中的节点又具体分为以下几方面内容,分别是声明、注释、节点以及节点间的文本,因此在TiXmlNode的基础上又衍生出这几个类TiXmlComment、TiXmlDeclaration、TiXmlDocument、TiXmlElement、TiXmlText、TiXmlUnknown,分别用来指明具体是xml中的哪一部分。TiXmlAttribute类不同于TiXmlNode,它指的是在尖括号里面的内容,像<... ***=...>,其中***就是一个属性。这块我具体用一个xml文档说明一下,内容如下: 

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <phonebook>  
 3     <!--one item behalfs one contacted person.-->  
 4     <item>  
 5         <name>miaomaio</name>  
 6     <addr>Shaanxi Xi'an</addr>  
 7     <tel>13759911917</tel>  
 8     <email>miaomiao@home.com</email>  
 9     </item>  
10     <item>  
11         <name>gougou</name>  
12     <addr>Liaoning Shenyang</addr>  
13     <tel>15840330481</tel>  
14     <email>gougou@home.com</email>  
15     </item>  
16     <!--more contacted persons.-->  
17 </phonebook>  
  • 像TiXmlDeclaration指的就是<?xml version="1.0" encoding="UTF-8"?>,
  • 像TiXmlComment指的就是<!--one item behalfs one contacted person.-->、 <!--more contacted persons.-->,
  • 像TiXmlDocument指的就是整个xml文档,
  • 像TiXmlElement指的就是<phonebook>、<item>、<name>、<addr>等等这些节点,
  • 像TiXmlText指的就是‘gougou’、‘15840330481’这些夹在<item>与</item>、<name>与</name>、<addr>与</addr>之间的文本文字,
  • 像TiXmlAttribute指的就是<?xml version="1.0" encoding="UTF-8"?>节点中version、encoding,
  • 除此之外就是TiXmlUnknown。

如何析xml文档,一般是这样的:

1 加载xml

2 获得根元素

3 获取任意一个元素

4 对元素进行解析。

 1 //______________________________________________________________________  
 2 // Read information from xml file.  
 3   
 4 // define xml file path, as follow , we use relative path,  
 5 // but you can use absolute path also.  
 6 const char* filepath = "phonebookdata.xml";  
 7 TiXmlDocument doc(filepath);  
 8 bool loadOkay = doc.LoadFile();  
 9 // faile to load 'phonebookdata.xml'.  
10 if (!loadOkay) {      
11     printf( "Could not load test file %s. Error='%s'. Exiting.
", filepath,doc.ErrorDesc() );  
12     exit( 1 );  
13 }  
14   
15 // get dom root of 'phonebookdata.xml', here root should be 'phonebook'.  
16 TiXmlElement* root = doc.RootElement();  
17   
18 printf("_______________________________________

");  
19 printf("     contacted person information      

");  
20 // trace every items below root.  
21 for( TiXmlNode*  item = root->FirstChild( "item" );  
22          item;  
23          item = item->NextSibling( "item" ) ) {  
24     printf("_______________________________________
");  
25   
26     // read name.  
27     TiXmlNode* child = item->FirstChild();  
28     const char* name = child->ToElement()->GetText();  
29     if (name) {  
30         printf("name:%s
",name);  
31     } else {  
32         printf("name:
");  
33     }  
34   
35     // read address.  
36     child = item->IterateChildren(child);  
37     const char* addr = child->ToElement()->GetText();  
38     if (addr) {  
39         printf("addr:%s
",addr);  
40     } else {  
41         printf("addr:
");  
42     }  
43   
44   
45     // read telephone no.  
46     child = item->IterateChildren(child);  
47     const char* tel = child->ToElement()->GetText();  
48         if (tel) {  
49         printf("tel:%s
",tel);  
50     } else {  
51         printf("tel:
");  
52     }  
53   
54     // read e-mail.  
55     child = item->IterateChildren(child);  
56     const char* email = child->ToElement()->GetText();  
57     if(email) {  
58         printf("email:%s
",email);  
59     } else {  
60         printf("email:
");  
61     }  
62       
63     printf("
");  
64   
65 }  
66 //______________________________________________________________________  
67   
68   
69 //______________________________________________________________________  
70 // Add information to xml file and save it.  
71 TiXmlElement* writeRoot = doc.RootElement();  
72 TiXmlNode* newNode = new TiXmlElement("item");  
73   
74    const TiXmlNode* name4NewNode = new TiXmlElement("name");  
75 newNode->InsertEndChild(*name4NewNode)->InsertEndChild(TiXmlText("pipi"));  
76   
77 const TiXmlNode* addr4NewNode = new TiXmlElement("addr");  
78 newNode->InsertEndChild(*addr4NewNode)->InsertEndChild(TiXmlText("Shaanxi Xianyang"));  
79   
80 const TiXmlNode* tel4NewNode = new TiXmlElement("tel");  
81 newNode->InsertEndChild(*tel4NewNode)->InsertEndChild(TiXmlText("02937310627"));  
82   
83 const TiXmlNode* email4NewNode = new TiXmlElement("email");  
84 newNode->InsertEndChild(*email4NewNode)->InsertEndChild(TiXmlText("pipi@home.com"));  
85   
86 writeRoot->InsertEndChild(*newNode);  
87 doc.SaveFile();  
88 //______________________________________________________________________  

 利用Parse方法可以方便的对xml字符串进行解析

 1 const char* lpszCommand;
 2 
 3 TiXmlDocument lActionXML;
 4         lActionXML.Parse(lpszCommand);
 5         //获得Action节点
 6         const TiXmlNode* lpParamActionNode = NULL;
 7         lpParamActionNode = lActionXML.RootElement();
 8         const TiXmlNode* lpItemItemNode = NULL;
 9 
10         while(lpItemItemNode = lpParamActionNode->ToElement()->IterateChildren(STANDARD_XML_ITEM_ITEM,lpItemItemNode))
11         {
12             m_strDeviceID= lpItemItemNode->ToElement()->Attribute(STANDARD_XML_ITEM_ID);
13             break;
14         }
原文地址:https://www.cnblogs.com/shmilxu/p/4831312.html