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

