首页 > 编程学习 > Qt中xml文件的读写接口类

Qt中xml文件的读写接口类

发布时间:2022/1/17 12:29:27

转自  http://www.oschina.net/code/snippet_195885_9703

[文件] xprojectreader.h ~ 557B    下载(65)

view source
 
print ?
01#ifndef XPROJECTREADER_H
02#define XPROJECTREADER_H
03  
04#include <vector>
05#include <QtXml/QtXml>
06#include "global.h"
07class QString;
08  
09class CXProjectReader
10{
11public:
12    CXProjectReader(vector<STask> *);
13  
14    bool read(QIODevice *device);
15    QString readProjectName() const;
16  
17private:
18    void readXProject(QDomElement);
19    void readTask(QDomElement);
20    ETASKTYPE toETaskType(QString);
21  
22    QDomDocument m_xml;
23    //QXmlStreamReader m_xml;
24    vector<STask> *m_pVProject;
25    QString m_projectName;
26};
27  
28#endif // XPROJECTREADER_H

[文件] xprojectreader.cpp ~ 4KB    下载(56)

view source
 
print ?
001#include "xprojectreader.h"
002#include <QtGui/QtGui>
003#include <QtCore/QtCore>
004  
005CXProjectReader::CXProjectReader(vector<STask> *project)
006    :m_pVProject(project)
007{
008  
009}
010  
011bool CXProjectReader::read(QIODevice *device)
012{    
013    QString errorStr;
014    int errorLine;
015    int errorColumn;
016  
017    if (!m_xml.setContent(device, true, &errorStr, &errorLine,
018                                &errorColumn)) 
019    {
020    //QMessageBox::information(window(), QObject::tr("DOM Project"),
021 //                                QObject::tr("Parse error at line %1, column %2:\n%3")
022 //                                .arg(errorLine)
023 //                                .arg(errorColumn)
024 //                                .arg(errorStr));
025        return false;
026    }
027  
028    QDomElement root = m_xml.documentElement();
029    if (root.tagName() != "project"
030    {
031        //QMessageBox::information(window(), QObject::tr("DOM Project"),
032        //                         QObject::tr("The file is not an xml file."));
033        return false;
034    
035    else if (root.hasAttribute("version")
036               && root.attribute("version") != "1.0"
037    {
038        //QMessageBox::information(window(), QObject::tr("DOM Project"),
039        //                         QObject::tr("The file is not an xml version 1.0 "
040        //                            "file."));
041        return false;
042    }
043  
044    m_projectName = root.attribute("name");
045    readXProject(root);
046  
047    return true;
048}
049  
050void CXProjectReader::readXProject(QDomElement root)
051{
052    //QDomElement root = m_xml.documentElement();
053    QDomElement child = root.firstChildElement("task");
054    while (!child.isNull()) 
055    {
056        readTask(child);
057        child = child.nextSiblingElement("task");
058    }
059}
060  
061void CXProjectReader::readTask(QDomElement element)
062{
063    STask sTask;
064    sTask.type = toETaskType(element.attribute("name"));
065  
066    QDomElement child = element.firstChildElement();
067    while (!child.isNull())
068    {
069        if (child.tagName() == "readonly")
070            sTask.readOnly = child.text().toInt();
071        else if (child.tagName() == "value")
072            sTask.value = child.text().toStdString();
073        else if (child.tagName() == "mistake")
074            sTask.mistake = child.text().toFloat();
075        else if (child.tagName() == "keyitem")
076            sTask.keyItem = child.text().toInt();
077        else if (child.tagName() == "vrscope")
078            sTask.vrScope = child.text().toInt();
079  
080        child = child.nextSiblingElement();
081    }
082  
083    m_pVProject->push_back(sTask);
084}
085  
086ETASKTYPE CXProjectReader::toETaskType(QString name)
087{
088    if(name == "MARK100")
089        return MARK100; //100V��ѹ�ı궨
090    else if(name == "MARK220")
091        return MARK220; //220V��ѹ�ı궨
092    else if(name == "MARK380")
093        return MARK380; //380V��ѹ�ı궨
094    else if(name == "OC100")
095        return OC100;   //100V��ѹ��OC��
096    else if(name == "OC220")
097        return OC220;   //220V��ѹ��OC��
098    else if(name == "OC380")
099        return OC380;   //380V��ѹ��OC��
100    else if(name == "PRECISION100")
101        return PRECISION100; //100V��ѹ�ľ��ȼ��
102    else if(name == "PRECISION220")
103        return PRECISION220; //220V��ѹ�ľ��ȼ��
104    else if(name == "PRECISION380")
105        return PRECISION380; //380V��ѹ�ľ��ȼ��
106    else if(name == "CLOCKMISTAKE")
107        return CLOCKMISTAKE; //ʱ�����
108    else if(name == "COMM")
109        return COMM;         //ͨѶ��ʽ��IP-PORT,192.1.1.81-8080
110    else if(name == "AREATERMCODE")
111        return AREATERMCODE;     //�ն�������,ʮ�������
112//    else if(name == "TERMCODE")
113//        return TERMCODE;     //�ն˵�ַ
114    else if(name == "VOLTRANKLIMIT")
115        return VOLTRANKLIMIT;//��ѹ�ȼ���������,100-7-10
116    else if(name == "APN")
117        return APN;         //APN,CMNET��CMWAP
118    else if(name == "HEARTBEAT")
119        return HEARTBEAT;    //������
120    else if(name == "TERMTIME")
121        return TERMTIME;    //�ն�ʱ��
122    else if(name == "FOOTDAY")
123        return FOOTDAY;     //������
124    else if(name == "POWEROFFCLOCK")
125        return POWEROFFCLOCK;//�����ʱ�Ӽ��͵�ѹ���
126    else if(name == "SOFTVERSION")
127        return SOFTVERSION;  //�ն�����汾
128}
129  
130QString CXProjectReader::readProjectName() const
131{
132    return m_projectName;
133}

[文件] xprojectwriter.h ~ 522B    下载(49)

view source
 
print ?
01#ifndef XPROJECTWRITER_H
02#define XPROJECTWRITER_H
03  
04#include <QtXml/QDomDocument>
05#include "global.h"
06class QString;
07  
08class CXProjectWriter
09{
10public:
11    CXProjectWriter(vector<STask> *);
12  
13    bool writeFile(QIODevice *device);
14    void writeProjectName(QString);
15  
16private:
17    void writeTask(STask, QDomElement&);
18    QString fromTaskType(ETASKTYPE);
19  
20    QDomDocument m_xml;
21    //QXmlStreamWriter m_xml;
22    vector<STask> *m_pVProject;
23    QString m_projectName;
24};
25  
26#endif // XPROJECTWRITER_H

[文件] xprojectwriter.cpp ~ 4KB    下载(55)

view source
 
print ?
001#include "xprojectwriter.h"
002#include <QtGui/QtGui>
003  
004CXProjectWriter::CXProjectWriter(vector<STask> *project)
005    :m_pVProject(project)
006{
007    //m_xml.setAutoFormatting(true);
008}
009  
010bool CXProjectWriter::writeFile(QIODevice *device)
011{
012    //QDomElement root = m_xml.documentElement();
013    //root.setTagName("project"); 
014    QDomElement root = m_xml.createElement("project");
015    root.setAttribute("version", "1.0");
016    root.setAttribute("name", m_projectName);
017    m_xml.appendChild(root);
018  
019    for (uint i = 0; i < m_pVProject->size(); ++i)
020        writeTask(m_pVProject->at(i), root);
021  
022    const int IndentSize = 4;
023  
024    QTextStream out(device);
025    m_xml.save(out, IndentSize);
026    return true;
027}
028  
029void CXProjectWriter::writeTask(STask task, QDomElement& root)
030{
031    QDomElement taskElement = m_xml.createElement("task");
032    QString taskName = fromTaskType(task.type);  
033    taskElement.setAttribute("name", taskName);
034    root.appendChild(taskElement);
035  
036    QDomElement readElement = m_xml.createElement("readonly");
037    QDomText readText = m_xml.createTextNode(QString::number(task.readOnly));
038    readElement.appendChild(readText);
039  
040    QDomElement valueElement = m_xml.createElement("value");
041    QDomText valueText = m_xml.createTextNode(QString::fromStdString(task.value));
042    valueElement.appendChild(valueText);
043  
044    QDomElement mistakeElement = m_xml.createElement("mistake");
045    QDomText mistakeText = m_xml.createTextNode(QString::number(task.mistake));
046    mistakeElement.appendChild(mistakeText);
047  
048    QDomElement keyitemElement = m_xml.createElement("keyitem");
049    QDomText keyitemText = m_xml.createTextNode(QString::number(task.keyItem));
050    keyitemElement.appendChild(keyitemText);
051  
052    QDomElement vrscopeElement = m_xml.createElement("vrscope");
053    QDomText vrscopeText = m_xml.createTextNode(QString::number(task.vrScope));
054    vrscopeElement.appendChild(vrscopeText);
055  
056    taskElement.appendChild(readElement);
057    taskElement.appendChild(valueElement);
058    taskElement.appendChild(mistakeElement);
059    taskElement.appendChild(keyitemElement);
060    taskElement.appendChild(vrscopeElement);
061}
062  
063QString CXProjectWriter::fromTaskType(ETASKTYPE type)
064{
065    if(type == MARK100)
066        return "MARK100"; //100V��ѹ�ı궨
067    else if(type == MARK220)
068        return "MARK220"; //220V��ѹ�ı궨
069    else if(type == MARK380)
070        return "MARK380"; //380V��ѹ�ı궨
071    else if(type == OC100)
072        return "OC100";   //100V��ѹ��OC��
073    else if(type == OC220)
074        return "OC220";   //220V��ѹ��OC��
075    else if(type == OC380)
076        return "OC380";   //380V��ѹ��OC��
077    else if(type == PRECISION100)
078        return "PRECISION100"; //100V��ѹ�ľ��ȼ��
079    else if(type == PRECISION220)
080        return "PRECISION220"; //220V��ѹ�ľ��ȼ��
081    else if(type == PRECISION380)
082        return "PRECISION380"; //380V��ѹ�ľ��ȼ��
083    else if(type == CLOCKMISTAKE)
084        return "CLOCKMISTAKE"; //ʱ�����
085    else if(type == COMM)
086        return "COMM";         //ͨѶ��ʽ��IP-PORT,192.1.1.81-8080
087    else if(type == AREATERMCODE)
088        return "AREATERMCODE";     //�ն�������,ʮ�������
089//    else if(type == TERMCODE)
090//        return "TERMCODE";     //�ն˵�ַ
091    else if(type == VOLTRANKLIMIT)
092        return "VOLTRANKLIMIT";//��ѹ�ȼ���������,100-7-10
093    else if(type == APN)
094        return "APN";         //APN,CMNET��CMWAP
095    else if(type == HEARTBEAT)
096        return "HEARTBEAT";    //������
097    else if(type == TERMTIME)
098        return "TERMTIME";    //�ն�ʱ��
099    else if(type == FOOTDAY)
100        return "FOOTDAY";     //������
101    else if(type == POWEROFFCLOCK)
102        return "POWEROFFCLOCK";//�����ʱ�Ӽ��͵�ѹ���
103    else if(type == SOFTVERSION)
104        return "SOFTVERSION"//�ն�����汾
105  
106    return QString();
107}
108  
109void CXProjectWriter::writeProjectName(QString name)
110{
111    m_projectName = name;
112}

转载于:https://www.cnblogs.com/sherry-best/archive/2012/12/20/2826356.html

Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000