網站首頁 編程語言 正文
前言
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》中的例子
<?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文件:
<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元素下的最大最小坐標:
#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返回變量類型。
原文鏈接:https://blog.csdn.net/qq_36584673/article/details/123595330
相關推薦
- 2022-01-30 tortoisegit pull時報錯
- 2022-05-04 C語言中getchar()的原理以及易錯點解析_C 語言
- 2022-09-25 引入DjangoRESTframework
- 2022-09-18 iOS開發底層探索界面優化示例詳解_IOS
- 2022-06-29 pytorch部署到jupyter中的問題及解決方案_python
- 2022-06-27 python中SSH遠程登錄設備的實現方法_python
- 2022-04-03 詳解iOS?實現一對多代理方案_IOS
- 2022-07-10 簡單解析表格table標簽的用法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支