C++ 使用TinyXML解析XML文件


1.介紹

  讀取和設置xml配置文件是最常用的操作,TinyXML是一個開源的解析XML的C++解析庫,能夠在Windows或Linux中編譯。這個解析庫的模型通過解析XML文件,然后在內存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。  

  下載TinyXML的網址:http://www.grinninglizard.com/tinyxml/

  使用TinyXML只需要將其中的6個文件拷貝到項目中就可以直接使用了,這六個文件是:tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。

 2.讀取XML文件

 如讀取文件a.xml:

<ToDo>
    <Item priority="1"> 
        <bold> Book store!
        </bold>
    </Item>
    <Item priority="2"> book1 </Item>
    <Item priority="2"> book2 </Item>
</ToDo>

讀取代碼如下:

 1 #include "tinyxml.h"
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 enum SuccessEnum {FAILURE, SUCCESS};
 8 
 9 SuccessEnum loadXML()
10 {
11     TiXmlDocument doc;
12     if(!doc.LoadFile("a.xml"))
13     {
14         cerr << doc.ErrorDesc() << endl;
15         return FAILURE;
16     }
17 
18     TiXmlElement* root = doc.FirstChildElement();
19     if(root == NULL)
20     {
21         cerr << "Failed to load file: No root element." << endl;
22         doc.Clear();
23         return FAILURE;
24     }
25 
26     for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
27     {
28         string elemName = elem->Value();
29         const char* attr;
30         attr = elem->Attribute("priority");
31         if(strcmp(attr,"1")==0)
32         {
33             TiXmlElement* e1 = elem->FirstChildElement("bold");
34             TiXmlNode* e2=e1->FirstChild();
35             cout<<"priority=1\t"<<e2->ToText()->Value()<<endl;
36 
37         }
38         else if(strcmp(attr,"2")==0)
39         {
40             TiXmlNode* e1 = elem->FirstChild();
41             cout<<"priority=2\t"<<e1->ToText()->Value()<<endl;
42         }
43     }
44     doc.Clear();
45     return SUCCESS;
46 }
47 
48 int main(int argc, char* argv[])
49 {
50     if(loadXML() == FAILURE)
51         return 1;
52     return 0;
53 }
View Code

3.生成XML文件

 如生成文件b.xml如下所示:

<root>
    <Element1 attribute1="some value" />
    <Element2 attribute2="2" attribute3="3">
        <Element3 attribute4="4" />
        Some text.
    </Element2>
</root>

生成上面b.xmlL文件代碼如下:

 1 #include "tinyxml.h"
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 
 6 enum SuccessEnum {FAILURE, SUCCESS};
 7 
 8 SuccessEnum saveXML()
 9 {
10     TiXmlDocument doc;
11 
12     TiXmlElement* root = new TiXmlElement("root");
13     doc.LinkEndChild(root);
14 
15     TiXmlElement* element1 = new TiXmlElement("Element1");
16     root->LinkEndChild(element1);
17 
18     element1->SetAttribute("attribute1", "some value");
19 
20 
21     TiXmlElement* element2 = new TiXmlElement("Element2");  ///元素
22     root->LinkEndChild(element2);
23 
24     element2->SetAttribute("attribute2", "2");
25     element2->SetAttribute("attribute3", "3");
26 
27 
28     TiXmlElement* element3 = new TiXmlElement("Element3");
29     element2->LinkEndChild(element3);
30 
31     element3->SetAttribute("attribute4", "4");
32 
33     TiXmlText* text = new TiXmlText("Some text.");  ///文本
34     element2->LinkEndChild(text);
35 
36 
37     bool success = doc.SaveFile("b.xml");
38     doc.Clear();
39 
40     if(success)
41         return SUCCESS;
42     else
43         return FAILURE;
44 }
45 
46 int main(int argc, char* argv[])
47 {
48     if(saveXML() == FAILURE)
49         return 1;
50     return 0;
51 }
View Code

4.重要函數或類型的說明

  (1)FirstChildElement(const char* value=0):獲取第一個值為value的子節點,value默認值為空,則返回第一個子節點。

  (2)NextSiblingElement( const char* _value=0 ) :獲得下一個(兄弟)節點。

  (3)LinkEndChild(XMLHandle *handle):添加一個子節點。元素或者文本

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM