C++中TinyXML读取xml文件用法详解

2022-06-10 14:00:22
目录
前言XML文件理解常用的XML类方法使用总结

前言

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>

<?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:JPEGImages409.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返回变量类型。

       到此这篇关于C++中TinyXML读取xml文件用法详解的文章就介绍到这了,更多相关C++>