日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

作者:18歲小白想成大牛 ? 更新時間: 2022-08-03 編程語言

前言

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

欄目分類
最近更新