欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中TinyXML讀取xml文件用法詳解

 更新時(shí)間:2022年06月10日 11:21:04   作者:18歲小白想成大牛  
本文主要介紹了C++中TinyXML讀取xml文件用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

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)文章

最新評(píng)論