QT parsing XML (mechanical disassembly)

Source: Internet
Author: User

Directly on the code:

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;//Translational
Lvector m_roate;//Rotation
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 (ro W, + 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!\n"). 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!\n"). 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;
}

QT parsing XML (mechanical disassembly)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.