温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中怎么使用TinyXML读取xml文件

发布时间:2022-06-10 13:44:08 来源:亿速云 阅读:419 作者:iii 栏目:开发技术

这篇文章主要介绍了C++中怎么使用TinyXML读取xml文件的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C++中怎么使用TinyXML读取xml文件文章都会有所收获,下面我们一起来看看吧。

前言

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;
}

关于“C++中怎么使用TinyXML读取xml文件”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“C++中怎么使用TinyXML读取xml文件”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI