前言
TinyXML下载地址:https://sourceforge.net/projects/tinyxml/
官方文档:TinyXML
TinyXML是个解析库,它由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)导入工程就可以用它的东西了。如果需要,可以将它做成自己的DLL来调用。
XML文件理解
举一个官方文档《TinyXML Tutorial》中的例子
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" ?> < MyApp > <!-- Settings for MyApp --> < Messages > < Welcome >Welcome to MyApp</ Welcome > < Farewell >Thank you for using MyApp</ Farewell > </ Messages > < Windows > < Window name = "MainFrame" x = "5" y = "15" w = "400" h = "250" /> </ Windows > < Connection ip = "192.168.0.1" timeout = "123.456000" /> </ MyApp > |
XML是树形结构,有层数之分,其结点分为不同的类别,而TinyXML中针对不同类别定义了不同的类,下面简单介绍一下:(粗体是常用的)
- <?xml version="1.0" ?>,TiXmlDeclaration,声明类
- <MyApp>,TiXmlElement,元素类,该结点是根节点,后续的每个<></>都是一个结点
- <!-- Settings for MyApp -->,TiXmlComment,注释类
- Welcome to MyApp,TiXmlText,文本类,获取元素中的文本
- TiXmlAttribute,属性类,name,x,y,w,h都是Window元素的属性
常用的XML类方法使用
接下来我们以一个目标检测的标签文件为例,来读取其中的boundingbox坐标信息。
XML文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
< annotation > < folder >JPEGImages</ folder > < filename >409.bmp</ filename > < path >E:\JPEGImages\409.bmp</ path > < source > < database >Unknown</ database > </ source > < size > < width >847</ width > < height >419</ height > < depth >3</ depth > </ size > < segmented >0</ segmented > < object > < name >bad_part</ name > < pose >Unspecified</ pose > < truncated >0</ truncated > < difficult >0</ difficult > < bndbox > < xmin >512</ xmin > < ymin >153</ ymin > < xmax >693</ xmax > < ymax >325</ ymax > </ bndbox > </ object > < object > < name >bad_part</ name > < pose >Unspecified</ pose > < truncated >0</ truncated > < difficult >0</ difficult > < bndbox > < xmin >251</ xmin > < ymin >251</ ymin > < xmax >321</ xmax > < ymax >313</ ymax > </ bndbox > </ object > </ annotation > |
文件中有两个boundingbox
获取bndbox元素下的最大最小坐标:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include <iostream> //打开xml文件需要加载的头文件 #include "tinystr.h" #include "tinyxml.h" #include <string> #include<typeinfo> using namespace std; int main() { //创建xml文件对象,并读取xml TiXmlDocument doc; doc.LoadFile( "409.xml" ); //获取xml中根元素,并输出根节点的值,为<annotation> TiXmlElement *root = doc.FirstChildElement(); cout << root->Value() << endl; //获取根节点孩子,输出节点值,输出节点的内容,Text是char* TiXmlElement *child = root->FirstChildElement(); cout << child->Value() << endl; cout << child->GetText() << endl; cout << strlen (child->GetText())<< endl; //cout <<typeid(child->GetText()).name()<< endl; /*目标:找到xmin,xmax,ymin,ymax*/ int xmin1,ymin1,xmax1,ymax1; //从根节点的第一个孩子节点开始遍历 while (child!=NULL) { if (child->ValueTStr() == "object" ) { TiXmlElement *box = child->FirstChildElement(); while (box->ValueTStr()!= "bndbox" ) { box = box->NextSiblingElement(); } TiXmlElement *xmin = box->FirstChildElement(); xmin1 = atoi (xmin->GetText()); //NextSiblingElement()获得同一层下一个节点 TiXmlElement *ymin = xmin->NextSiblingElement(); ymin1 = atoi (ymin->GetText()); TiXmlElement *xmax = ymin->NextSiblingElement(); xmax1 = atoi (xmax->GetText()); TiXmlElement *ymax = xmax->NextSiblingElement(); ymax1 = atoi (ymax->GetText()); cout<<xmin1<<endl; cout<<ymin1<<endl; cout<<xmax1<<endl; cout<<ymax1<<endl; } child = child->NextSiblingElement(); } /* cout<<xmin1<<endl; cout<<ymin1<<endl; cout<<xmax1<<endl; cout<<ymax1<<endl; */ /*一些其他方法的测试*/ /* //获取兄弟节点中的size节点 TiXmlElement *brother = child->NextSiblingElement("size"); cout << brother->Value() << endl; //cout << typeid(brother->GetText()).name()<< endl; //获取size节点下的属性值,<>中的属性,本例没有属性 //cout <<brother->Attribute("width")<<endl; //找size下面节点width TiXmlElement *brother_child = brother->FirstChildElement(); cout << brother_child->Value() << endl; cout << brother_child->GetText() << endl; //读取到内容,并转为int型,因为项目需要int数据 int width = atoi(brother_child->GetText()); cout << width << endl; */ return 0; } |
总结
- 主要是链表相关知识。
- 常用的解析xml的方法。
- char*转int类型用atoi,转float用atof,typeid返回变量类型。
到此这篇关于C++中TinyXML读取xml文件用法详解的文章就介绍到这了,更多相关C++ TinyXML读取xml内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_36584673/article/details/123595330