C++中TinyXML讀取xml文件用法詳解
前言
TinyXML下載地址:https://sourceforge.net/projects/tinyxml/
官方文檔:TinyXML
TinyXML是個(gè)解析庫(kù),它由兩個(gè)頭文件(.h文件)和四個(gè)CPP文件(.cpp文件)構(gòu)成,用的時(shí)候,只要將(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)導(dǎo)入工程就可以用它的東西了。如果需要,可以將它做成自己的DLL來(lái)調(diào)用。
XML文件理解
舉一個(gè)官方文檔《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是樹形結(jié)構(gòu),有層數(shù)之分,其結(jié)點(diǎn)分為不同的類別,而TinyXML中針對(duì)不同類別定義了不同的類,下面簡(jiǎn)單介紹一下:(粗體是常用的)
- <?xml version="1.0" ?>,TiXmlDeclaration,聲明類
- <MyApp>,TiXmlElement,元素類,該結(jié)點(diǎn)是根節(jié)點(diǎn),后續(xù)的每個(gè)<></>都是一個(gè)結(jié)點(diǎn)
- <!-- Settings for MyApp -->,TiXmlComment,注釋類
- Welcome to MyApp,TiXmlText,文本類,獲取元素中的文本
- TiXmlAttribute,屬性類,name,x,y,w,h都是Window元素的屬性
常用的XML類方法使用
接下來(lái)我們以一個(gè)目標(biāo)檢測(cè)的標(biāo)簽文件為例,來(lái)讀取其中的boundingbox坐標(biāo)信息。
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>
文件中有兩個(gè)boundingbox
獲取bndbox元素下的最大最小坐標(biāo):
#include <iostream> //打開xml文件需要加載的頭文件 #include "tinystr.h" #include "tinyxml.h" #include <string> #include<typeinfo> using namespace std; int main() { ? ? //創(chuàng)建xml文件對(duì)象,并讀取xml ? ? TiXmlDocument doc; ? ? doc.LoadFile("409.xml"); ? ? //獲取xml中根元素,并輸出根節(jié)點(diǎn)的值,為<annotation> ? ? TiXmlElement *root = doc.FirstChildElement(); ? ? cout << root->Value() << endl; ? ? //獲取根節(jié)點(diǎn)孩子,輸出節(jié)點(diǎn)值,輸出節(jié)點(diǎn)的內(nèi)容,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; ? ? /*目標(biāo):找到xmin,xmax,ymin,ymax*/ ? ? int xmin1,ymin1,xmax1,ymax1; ? ? //從根節(jié)點(diǎn)的第一個(gè)孩子節(jié)點(diǎn)開始遍歷 ? ? 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()獲得同一層下一個(gè)節(jié)點(diǎn) ? ? ? ? ? ? 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; ? ? */ ? ?? ? ? /*一些其他方法的測(cè)試*/ ? ? /* ? ? //獲取兄弟節(jié)點(diǎn)中的size節(jié)點(diǎn) ? ? TiXmlElement *brother = child->NextSiblingElement("size"); ? ? cout << brother->Value() << endl; ? ? //cout << typeid(brother->GetText()).name()<< endl; ? ? //獲取size節(jié)點(diǎn)下的屬性值,<>中的屬性,本例沒(méi)有屬性 ? ? //cout <<brother->Attribute("width")<<endl; ? ? //找size下面節(jié)點(diǎn)width ? ? TiXmlElement *brother_child = brother->FirstChildElement(); ? ? cout << brother_child->Value() << endl; ? ? cout << brother_child->GetText() << endl; ? ? //讀取到內(nèi)容,并轉(zhuǎn)為int型,因?yàn)轫?xiàng)目需要int數(shù)據(jù) ? ? int width = atoi(brother_child->GetText()); ? ? cout << width << endl; ? ? */ ? ? return 0; }
總結(jié)
- 主要是鏈表相關(guān)知識(shí)。
- 常用的解析xml的方法。
- char*轉(zhuǎn)int類型用atoi,轉(zhuǎn)float用atof,typeid返回變量類型。
到此這篇關(guān)于C++中TinyXML讀取xml文件用法詳解的文章就介紹到這了,更多相關(guān)C++ TinyXML讀取xml內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于matlab對(duì)比度和結(jié)構(gòu)提取的多模態(tài)解剖圖像融合實(shí)現(xiàn)
這篇文章主要介紹了多模態(tài)醫(yī)學(xué)圖像配準(zhǔn)與融合的概念、方法及意義,最后簡(jiǎn)單介紹了小波變換分析方法。感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-11-11圖解AVL樹數(shù)據(jù)結(jié)構(gòu)輸入與輸出及實(shí)現(xiàn)示例
這篇文章主要為大家介紹了C++圖解AVL樹數(shù)據(jù)結(jié)構(gòu)輸入與輸出操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Java C++ 題解leetcode857雇傭K名工人最低成本vector pair
這篇文章主要為大家介紹了Java C++ 題解leetcode857雇傭K名工人最低成本vector pair示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09C++工廠方法之對(duì)象創(chuàng)建型模式詳解
這篇文章主要為大家詳細(xì)介紹了C++對(duì)象創(chuàng)建型模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03C語(yǔ)言使用openSSL庫(kù)AES模塊實(shí)現(xiàn)加密功能詳解
這篇文章主要介紹了C語(yǔ)言使用openSSL庫(kù)AES模塊實(shí)現(xiàn)加密功能,詳細(xì)分析了C語(yǔ)言加密的相關(guān)概念、原理及AES模塊加密具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù)詳解
bind是一組用于函數(shù)綁定的模板。在對(duì)某個(gè)函數(shù)進(jìn)行綁定時(shí),可以指定部分參數(shù)或全部參數(shù),也可以不指定任何參數(shù),還可以調(diào)整各個(gè)參數(shù)間的順序。這篇文章主要介紹了c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù) ,需要的朋友可以參考下2018-09-09C++11計(jì)時(shí)器之chrono庫(kù)簡(jiǎn)介
C++11有了chrono庫(kù),可以在不同系統(tǒng)中很容易的實(shí)現(xiàn)定時(shí)功能,要使用chrono庫(kù),需要#include,其所有實(shí)現(xiàn)均在std::chrono namespace下,本文給大家介紹C++11計(jì)時(shí)器:chrono庫(kù)介紹,感興趣的朋友一起看看吧2023-12-12C語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 雙向鏈表的建立與基本操作
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu) 雙向鏈表的建立與基本操作的相關(guān)資料,需要的朋友可以參考下2017-03-03C++/Php/Python/Shell 程序按行讀取文件或者控制臺(tái)的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇C++/Php/Python/Shell 程序按行讀取文件或者控制臺(tái)的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03