在.pro文件中添加以下内容
QT += xml
实现 -- 以单个文件作为示例
#include "mainwindow.h"
#include <QApplication>
void parseXml(const QString & filePath);
int main(int argc, char * argv[])
{
QApplication a(argc, argv);
// MainWindow w;
// w.show();
QString xmlPath = ".../VOC2012_train_val/Annotations/2012_002880.xml";
parseXml(xmlPath);
return a.exec();
}
#include <QFile>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
void parseXml(const QString & filePath)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "Failed to open file:" << filePath;
return;
}
QDomDocument document;
if (!document.setContent(&file))
{
qDebug() << "Failed to parse XML file.";
return;
}
QDomElement root = document.documentElement();
QDomNodeList objects = root.elementsByTagName("object");
for (int i = 0; i < objects.size(); ++i)
{
QDomElement object = objects.at(i).toElement();
// 获取物体名称
QDomElement nameElement = object.elementsByTagName("name").at(0).toElement();
QString name = nameElement.text();
qDebug() << "Object Name:" << name;
// 获取包含框信息
QDomElement bndboxElement = object.elementsByTagName("bndbox").at(0).toElement();
QDomElement xminElement = bndboxElement.elementsByTagName("xmin").at(0).toElement();
QDomElement yminElement = bndboxElement.elementsByTagName("ymin").at(0).toElement();
QDomElement xmaxElement = bndboxElement.elementsByTagName("xmax").at(0).toElement();
QDomElement ymaxElement = bndboxElement.elementsByTagName("ymax").at(0).toElement();
int xmin = xminElement.text().toInt();
int ymin = yminElement.text().toInt();
int xmax = xmaxElement.text().toInt();
int ymax = ymaxElement.text().toInt();
qDebug() << "Bounding Box:" << xmin << ymin << xmax << ymax;
}
}
Top comments (0)