QT解析xml文件

mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "QTreeWidgetItem"
#include "ui_mainwindow.h"
#include <QDomDocument>
#include <QFile>
#include <QtDebug>
MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
//    ,
//    ui(new Ui::MainWindow)
{
    //    ui->setupUi(this);
    setupUi(this);
    initTable();
    initTree();
    setStyle();
    xmlAnalysis();
}

MainWindow::~MainWindow()
{
    //    delete ui;
}

void MainWindow::ShowPointDetail(int r, int c)
{
    qDebug() << r << c;
}

void MainWindow::initTable()
{
    connect(tableWidget, &QTableWidget::cellDoubleClicked, this, &MainWindow::ShowPointDetail);
    tableWidget->setRowCount(5);
    tableWidget->setColumnCount(5);
    QTableWidgetItem* item;
    qDebug() << tableWidget->rowCount();
    qDebug() << tableWidget->columnCount();
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            item = new QTableWidgetItem(QString::number(j + 1));
            tableWidget->setItem(j, 0, item);

            qDebug() << "ces";

            item = new QTableWidgetItem("12000");
            tableWidget->setItem(j, 1, item);
        }
    }
    //    tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch);
    tableWidget->horizontalHeader()->setStretchLastSection(true);
    tableWidget->horizontalHeader()->setMinimumHeight(40);
    tableWidget->horizontalHeader()->setFont(QFont("song", 12));
    treeWidget->setColumnCount(12);
    qDebug() << treeWidget->columnCount();
}

void MainWindow::initTree()
{
    //    QTreeWidgetItem* tree = new QTreeWidgetItem(treeWidget,"test");
    QTreeWidgetItem* typeItem = new QTreeWidgetItem(treeWidget, QStringList(QString("test ")));
    typeItem->setText(1, "01");
    QTreeWidgetItem* typeItem1 = new QTreeWidgetItem(treeWidget, QStringList(QString("test1 ")));
    typeItem1->setText(2, "011");
    QTreeWidgetItem* typeItem2 = new QTreeWidgetItem(typeItem1, QStringList(QString("test12 ")));
    typeItem2->setText(1, "0112");
    treeWidget->addTopLevelItem(typeItem);
}
void MainWindow::xmlAnalysis()
{
    QFile f("D:\\naru\\baogao\\STAT_01_20230608_170625_552.xml");
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << tr("open error");
        return;
    }
    //    QByteArray arr = f.readAll();
    //    qDebug() << arr;
    qDebug() << "anasysis STA01";
    qDebug() << f.fileName() << f.fileName().toLocal8Bit().data();
    qDebug() << f.fileName().toUtf8().data();
    QString errStr;
    int errLine;
    int errColoum;
    QDomDocument doc;
    if (!doc.setContent(&f, false, &errStr, &errLine, &errColoum)) {
        qPrintable("Error:Parser er");
        qPrintable(errStr);
    }

    QDomElement root = doc.documentElement();
    qDebug() << root.tagName().toUtf8().data();
    qDebug() << root.attribute("type") << root.attribute("desc");
    QDomNodeList query_node = root.elementsByTagName("QueryTime");
    qDebug() << query_node.count() << query_node.isEmpty() << query_node.size() << query_node.length();

    if (query_node.count() == 1) {
        //查询节点1
        QDomNode m = query_node.item(0);
        qDebug() << m.toElement().attribute("start");
        //警告节点

        QDomNodeList warn_node = m.toElement().elementsByTagName("Warning");
        qDebug() << "warning size = " << warn_node.count();
        for (int i = 0; i < warn_node.count(); i++) {
            qDebug() << warn_node.at(i).toElement().attribute("type"); //获取属性
            qDebug() << warn_node.at(i).toElement().attribute("time");
            // led节点

            QDomNodeList led_node = warn_node.item(i).toElement().elementsByTagName("IED"); //在当前节点获取下面的节点
            qDebug() << "led_node.count()" << led_node.count();
            for (int i = 0; i < led_node.count(); i++) {
                IED01 ied;
                led_node.item(i).toElement().attribute("name");
                ied.desc = led_node.item(i).toElement().attribute("desc");
                // point
                QDomNodeList point_node = led_node.item(i).toElement().elementsByTagName("Point"); //解析Point节点
                for (int i = 0; i < point_node.count(); i++) {
                    Point01 point;
                    point.path = point_node.item(i).toElement().attribute("path");
                    point.desc = point_node.item(i).toElement().attribute("desc");
                    point.other = point_node.item(i).toElement().attribute("other");
                    point.value = point_node.item(i).toElement().attribute("Value");
                    point.time = point_node.item(i).toElement().attribute("Time");
                    point.quality = point_node.item(i).toElement().attribute("Quality");
                    qDebug() << point.path;
                }
            }
            // Locations节点

            QDomNodeList loca_node = warn_node.item(i).toElement().elementsByTagName("Locations"); //解析Location节点
            qDebug() << "loca_node.count()" << loca_node.count();
            // location下的子节点
            QDomNodeList Ied_node = loca_node.at(0).toElement().elementsByTagName("Ied");
            qDebug() << "Ied_node.iedDesc()" << Ied_node.item(0).toElement().attribute("iedDesc");

            qDebug() << "Ied_node.count()" << Ied_node.count();

            // effect节点
            QDomNodeList effe_node = warn_node.item(i).toElement().elementsByTagName("Effects"); //解析Effects节点
            qDebug() << "effe_node.count()" << effe_node.count();

            qDebug() << effe_node.item(0).toElement().attribute("desc");
            //<Suggestions>
            QDomNodeList sugg_node = warn_node.item(i).toElement().elementsByTagName("Suggestions"); //解析Effects节点
            qDebug() << "sugg_node.count()" << sugg_node.count();

            qDebug() << sugg_node.item(0).toElement().attribute("desc");
        }

    } else {
        qDebug() << "QueryTime error";
    }
}

void MainWindow::setStyle()
{
    //    menufsda->setStyleSheet("background-color:red;");
    //    QMenuBar* bar = menuBar();
    //    bar->setStyleSheet("background-color:red;");
    setStyleSheet("QMenuBar{background-color: "
                  "qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, "
                  "stop:0 rgba(11, 126, 123, 255), "
                  "stop:1 rgba(25, 152, 149, 255));}"); // width:1920px;height:36px;
}

mainwindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

//#include "D:\\naru\\baogao\\relay_diagnose_lib\\relay_diagnose_struct.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
// namespace Ui {
// class MainWindow;
// }
//二次回路故障诊断 STAT_01 start
struct Point01 {
    QString path;
    QString desc;
    QString other;
    QString value;
    QString time;
    QString quality;
};

struct Cable {
    QString id;
    QString startIedName;
    QString startIedDesc;
    QString endIedName;
    QString endIedDesc;
    QString cableLen;
    QString resultId;
    QString priority;
};

struct Port {
    QString id;
    QString iedName;
    QString iedDesc;
    QString plug;
    QString resultId;
    QString priority;
};

struct Slot {
    QString id;
    QString iedName;
    QString iedDesc;
    QString resultId;
    QString priority;
};

struct Ied01 {
    QString iedName;
    QString iedDesc;
    QString resultId;
    QString priority;
};

struct Locations {
    QVector<Ied01> vec_Ied;
    QVector<Slot> vec_Slot;
    QVector<Port> vec_Port;
    QVector<Cable> vec_Cable;
};

struct IED01 {
    QString name;
    QString desc;
    QVector<Point01> vec_point;
};

struct Effects {
    QString desc;
    QString resultId;
    QString priority;
};

struct Suggestions {
    QString desc;
    QString resultId;
    QString priority;
};
struct Warning01 {
    QString type;
    QString time;
    QVector<IED01> vec_ied;
    Locations locations;
    QVector<Effects> vec_effect;
    QVector<Suggestions> vec_sugg;
};

struct QueryTime01 {
    QString start;
    QString end;
    QVector<Warning01> vec_warning;
};

struct STAT01 {
    QString type;
    QString desc;
    QueryTime01 query_time;
};

//二次回路故障诊断 STAT_01 end

class MainWindow : public QMainWindow, public Ui::MainWindow {
    Q_OBJECT

public:
    explicit MainWindow(QWidget* parent = NULL);
    ~MainWindow();
public slots:
    void ShowPointDetail(int r, int c);

private:
    //    Ui::MainWindow *ui;
    void initTable();
    void initTree();
    void setStyle();
    void xmlAnalysis();
};

#endif // MAINWINDOW_H
相关推荐
矛取矛求5 小时前
QT的前景与互联网岗位发展
开发语言·qt
Leventure_轩先生5 小时前
[WASAPI]从Qt MultipleMedia来看WASAPI
开发语言·qt
晓纪同学14 小时前
QT-简单视觉框架代码
开发语言·qt
威桑14 小时前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
飞飞-躺着更舒服14 小时前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
fyzy14 小时前
Qt获取本地计算的CPU温度
qt
cbdg375714 小时前
Qt 6 QML Settings location 不创建指定路径文件
qt
了一li14 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
杨德杰14 小时前
QT网络(一):主机信息查询
网络·qt
黄金右肾15 小时前
Qt之串口设计-线程实现(十二)
qt·thread·serialport