• QT解析XML(机械拆装)


    直接上代码:

    Xmlcommand.h


    #ifndef __XMLC_H
    #define __XMLC_H


    #include <QHash>
    #include <QList>
    #include <QDomDocument>
    #include <QFile>
    #include <QtGui>
    #include <QMessageBox>


    struct LVector{
    double x;
    double y;
    double z;
    };

    struct ActionUint{
    LVector m_Span;//平动
    LVector m_Roate;//旋转
    double m_Time;
    };

    struct Action{
    QList<ActionUint> m_Actions;
    };

    struct Step{
    QHash<QString, Action> m_OperateParts;
    };
    class GetdismountWayFromXML{
    public:
    GetdismountWayFromXML(QString file_name)
    {
    m_CurrentStepIndex = 0;
    m_filename = file_name;
    //m_NextStepIndex = 1;
    m_Count = 0;
    };
    void GetAllStep();
    int GetCurrentStepIndex();
    int GetStepCount();
    Step GetCurrentStep(Step *);
    //-----------------
    /*int GetNextStepIndex();
    QHash<QString, Action> GetNextStep();*/
    //
    bool StepDown();
    bool SetStep(int stepindex);
    ~GetdismountWayFromXML(){
    m_AllActions.clear();
    m_AllSteps.clear();
    };
    private:
    QList<Step> m_AllSteps;
    QList<Action> m_AllActions;
    QString m_filename;
    int m_Count;
    int m_CurrentStepIndex;
    //int m_NextStepIndex;
    bool GetActionFromXML(QString str,Action *);
    bool GetStepFromXML(QString str,Step *);
    int FindActionIndex(QString str);
    };


    #endif

    XMlCommand.cpp

    #include "XmlCommand.h"

    void GetdismountWayFromXML::GetAllStep()
    {
    QList<Step> qhashGetData;
    if (m_filename.isEmpty())
    {
    return;
    }
    QFile file(m_filename);
    if (!file.open(QFile::ReadOnly | QFile::Text))
    {
    QMessageBox::information(NULL, QString("title"), QString("OPen error"));
    return;
    }
    QDomDocument document;
    QString error;
    int row = 0;
    int column = 0;
    if (!document.setContent(&file, false, &error, &row, &column))
    {
    QMessageBox::information(NULL, QString("title"), QString("parse file failed at line row and column") + QString::number(row, 10) + QString(",") + QString::number(column, 10));

    return;
    }

    if (document.isNull())
    {
    QMessageBox::information(NULL, QString("title"), QString("document is null!"));

    return;
    }
    m_CurrentStepIndex = 0;
    m_Count = 0;
    m_AllActions.clear();
    m_AllSteps.clear();
    QDomElement root = document.documentElement();
    //actions
    QDomElement xmlactions = root.firstChildElement();
    QDomElement xmlaction = xmlactions.firstChildElement();
    while (!xmlaction.isNull())
    {
    QString strAction = xmlaction.text();
    Action gaction;
    GetActionFromXML(strAction,&gaction);
    m_AllActions << gaction;
    xmlaction = xmlaction.nextSiblingElement();
    }
    //steps
    QDomElement xmlsteps = xmlactions.nextSiblingElement();
    QDomElement xmlstep = xmlsteps.firstChildElement();
    while (!xmlstep.isNull())
    {
    QString strstep = xmlstep.text();
    Step gstep;
    GetStepFromXML(strstep,&gstep);
    m_AllSteps << gstep;
    m_Count++;
    xmlstep = xmlstep.nextSiblingElement();
    }
    }


    int GetdismountWayFromXML::GetStepCount()
    {
    return m_Count;
    }


    int GetdismountWayFromXML::GetCurrentStepIndex()
    {
    return m_CurrentStepIndex;
    }


    Step GetdismountWayFromXML::GetCurrentStep(Step *step)
    {
    if (m_Count > 0)
    {
    *step= m_AllSteps[m_CurrentStepIndex];
    }
    return *step;
    }

    bool GetdismountWayFromXML::StepDown()
    {
    m_CurrentStepIndex++;
    if (m_CurrentStepIndex > m_Count - 1)
    {
    m_CurrentStepIndex--;
    return false;
    }
    return true;
    }


    bool GetdismountWayFromXML::SetStep(int stepindex)
    {
    if (stepindex >= 0 && stepindex < m_Count)
    {
    m_CurrentStepIndex = stepindex;
    return true;
    }
    return false;
    }
    //int GetdismountWayFromXML::GetNextStepIndex(){}
    //
    //
    //Step GetdismountWayFromXML::GetNextStep(){}


    bool GetdismountWayFromXML::GetActionFromXML(QString str,Action *mac)
    {
    //Action mac;
    try
    {

    str = str.remove(QChar('('));
    str = str.remove(QChar(')'));
    QStringList st = str.split(QChar(';'));

    foreach (QString var , st)
    {
    QStringList strActionUnits = var.split(QChar(','));

    if (strActionUnits.length() > 0)
    {
    ActionUint au;
    au.m_Span.x = strActionUnits[0].toDouble();
    au.m_Span.y = strActionUnits[1].toDouble();
    au.m_Span.z = strActionUnits[2].toDouble();
    au.m_Roate.x = strActionUnits[3].toDouble();
    au.m_Roate.y = strActionUnits[4].toDouble();
    au.m_Roate.z = strActionUnits[5].toDouble();
    au.m_Time = strActionUnits[6].toDouble();
    mac->m_Actions << au;
    }
    //m_AllActions << mac;
    //au.m_Span.x
    }
    }
    catch (QException* e)
    {
    QMessageBox::information(NULL, QString("title"), QString("ParseXMlActionError! ").append(e->what()));
    return false;
    }
    return true;
    //Action a;

    }


    bool GetdismountWayFromXML::GetStepFromXML(QString str, Step *step)
    {
    //Step step;
    try
    {
    QStringList st = str.split(QChar(';'));
    foreach (QString var, st)
    {
    QStringList strStep= var.split(QChar(','));
    if (strStep.count() > 0)
    {
    int actindex = FindActionIndex(strStep[1]);
    if (actindex != 0)
    {
    step->m_OperateParts.insert(strStep[0], m_AllActions[actindex - 1]);
    }

    }
    }
    }
    catch (QException* e)
    {
    QMessageBox::information(NULL, QString("title"), QString("ParseXMlStepError! ").append(e->what()));
    return false;
    }
    return true;
    }


    int GetdismountWayFromXML::FindActionIndex(QString str)
    {
    int index = 0;
    if (m_AllActions.count() > 0)
    {
    index =str.mid(str.count()-1).toInt();
    }
    return index;
    }

  • 相关阅读:
    #Leetcode# 338. Counting Bits
    POJ 3614(Sunscreen)
    POJ 2392(Space Elevator)
    POJ 3666(Making the Grade)
    Coursera吴恩达《构建机器学习项目》课程笔记 -- 机器学习策略(下)
    Coursera吴恩达《构建机器学习项目》课程笔记-- 机器学习策略(上)
    深度学习入门--Stanford CS230--学习笔记
    POJ 1065 (Wooden Sticks)
    POJ 1742(Coins)
    C++ 按位与(&)、或(|)、异或(^)等操作符
  • 原文地址:https://www.cnblogs.com/ilovewuzhaoxia/p/4344639.html
Copyright © 2020-2023  润新知