lundi 20 avril 2015

Qt. XML(QDomDocument) log class is not working

I'm trying to build an XML log using QDomDocument. But, unfortunately it isn't working. I'm new with XML so I can't spot the problem. Here is my Log class.

#ifndef LOG_H
#define LOG_H

#include <QtXml/QtXml>
#include <QDate>
#include <QtXml/QDomDocument>

class Log
{
private:
    static Log* instance;
public:
    static Log* getInstance();
    ~Log();
    bool appendRecord(QString& _emp_id, QString& _emp_name, QString& _action, QDate _date);
private:
    Log();
    QDomElement makeRecord(QDomDocument& _doc, QString& _emp_id, QString& _emp_name, QString& _action, QDate _date);
    QDomElement makeElement(QDomDocument& _doc, QString& _name, QString& _attribute, QString& _text = QString());
};

#endif // LOG_H

And here is .cpp-file.

#include "log.h"
#include <QFile>

Log* Log::instance = nullptr;

Log::Log()
{
    qDebug() << Q_FUNC_INFO;
}

Log::~Log()
{
    qDebug() << Q_FUNC_INFO;
    if(instance)
        delete instance;
}

Log* Log::getInstance()
{
    qDebug() << Q_FUNC_INFO;
    if(!instance)
        instance = new Log();
    return instance;
}

bool Log::appendRecord(QString& _emp_id, QString& _emp_name, QString& _action, QDate _date)
{
    qDebug() << Q_FUNC_INFO;
    QDomDocument doc("log");
    QDomElement elem = doc.createElement("log");
    QDomElement record = makeRecord(doc, _emp_id, _emp_name, _action, _date);

    elem.appendChild(record);
    QFile file(QString("log_%1_%2_%3.xml").arg(_date.year()).arg(_date.month()).arg(_date.day()));
    if(file.open(QIODevice::WriteOnly))
    {
        QTextStream(&file) << doc.toString();
        QTextStream(&file) << "END.";
        file.close();
        return true;
    }
    return false;
}

QDomElement Log::makeRecord(QDomDocument& _doc, QString& _emp_id, QString& _emp_name, QString& _action, QDate _date)
{
    qDebug() << Q_FUNC_INFO;
    static int number = 1;

    QDomElement elem = makeElement(_doc, QString("record"), QString::number(number));
    elem.appendChild(makeElement(_doc, QString("ID"), QString(), _emp_id));
    elem.appendChild(makeElement(_doc, QString("name"), QString(), _emp_name));
    elem.appendChild(makeElement(_doc, QString("action"), QString(), _action));
    elem.appendChild(makeElement(_doc, QString("date"), QString(), _date.toString()));


    number++;
    return elem;
}

QDomElement Log::makeElement(QDomDocument& _doc, QString& _name, QString& _attribute, QString& _text)
{
    qDebug() << Q_FUNC_INFO;
    QDomElement elem = _doc.createElement(_name);

    if(!_attribute.isEmpty())
    {
        QDomAttr attr = _doc.createAttribute("number");
        attr.setValue(_attribute);
        elem.setAttributeNode(attr);
    }

    if(!_text.isEmpty())
    {
        QDomText txt = _doc.createTextNode(_text);
        qDebug() << _text;
        elem.appendChild(txt);
    }
    return elem;
}

Sorry, It's a bit messy, long and case-specific, but I think you'll get the idea. Thank you.

Aucun commentaire:

Enregistrer un commentaire