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):添加一个子节点。元素或者文本

 

posted on 2014-05-18 21:58  旭东的博客  阅读(53936)  评论(2编辑  收藏  举报

导航