QT DOM的方式读写Xml文件

来源:互联网 发布:java多线程并发控制 编辑:程序博客网 时间:2024/06/08 01:06

毕业以来做得时间最长的就是QT客户端程序开发了。在工作里经常遇到Xml文件的读写任务,为了以后省点事,所以抽出点时间写了个DOM方式读写Xml文件的类,因为时间的关系,有些方法没有做足够的测试,使用时间还需要认真测试测试。

头文件:

#ifndef XMLHELPER_H#define XMLHELPER_H#include <QDomDocument>#include <QStringList>class XmlHelper{public:    static XmlHelper* GetInstance();    bool LoadXmlFile(const QString& file_name);    bool CreateXmlDocument(const QString& file_name, const QString& root_node);    bool CreateXmlDocument(const QString& file_name,                           const QString& ip, const QString& port);    bool AddXmlNode(QDomElement& parent, const QString& node_tag, const QString& text);    bool AddXmlNode(const QString& parent_tag, const QString& node_tag, const QString& text = "");    bool RemoveXmlNode(const QString& node_tag);    bool ParseXmlDocument(const QString& file_name);    QString ParseXmlNodeContent(const QString& node_tag);    QStringList ParseXmlNodeList(const QString& node_tag);    QString ParseXmlNodeContent(const QDomElement& elem);    QString ParseXmlNodeContent(const QDomNode& special_node);    QString ParseXmlNodeAttribute(const QString& node_tag, const QString& attr_key);    QString ParseXmlNodeAttribute(const QDomElement& elem, const QString& attr_key);    bool SetXmlNodeContent(const QDomElement& elem, const QString& text);    bool SetXmlNodeContent(const QString& tag_name, const QString& text);    bool SetXmlNodeAttribute(const QString& node_tag,                             const QString& attr_key, const QString& attr_value);    QDomElement GetXmlRootNode();    QDomElement GetXmlNodeParent(const QString& node_tag);    bool ClearXmlDocument();    bool AddRootNode(const QString &root_node);private:    XmlHelper() {}    bool LoadXmlFile();private:    QDomDocument doc_;    QString file_name_;    static XmlHelper *helper_;};#endif // XMLHELPER_H

实现文件:

#include "xmlhelper.h"#include <QFile>#include <QTextStream>XmlHelper* XmlHelper::helper_ = NULL;XmlHelper *XmlHelper::GetInstance(){    if (helper_ == NULL)    {        helper_ = new XmlHelper;    }    return helper_;}bool XmlHelper::LoadXmlFile(const QString &file_name){    if (!QFile::exists(file_name))    {        return false;    }    QFile file(file_name);    file_name_ = file_name;    doc_.clear();    if (!file.open(QIODevice::ReadWrite))    {        file.close();        return false;    }    if (!doc_.setContent(&file))    {        file.close();        return false;    }    file.close();    return true;}bool XmlHelper::CreateXmlDocument(const QString &file_name, const QString& root_node){    qDebug() << file_name;    QFile file(file_name);    file_name_ = file_name;    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))    {        return false;    }    QDomProcessingInstruction instruction =            doc_.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");    doc_.appendChild(instruction);    QDomElement root = doc_.createElement(root_node);    doc_.appendChild(root);    QTextStream out(&file);    doc_.save(out,4);    file.close();    return true;}bool XmlHelper::CreateXmlDocument(const QString &file_name, const QString &ip, const QString &port){    QFile file(file_name);    file_name_ = file_name;    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))    {        return false;    }    QDomProcessingInstruction instruction =            doc_.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");    doc_.appendChild(instruction);    QDomElement root = doc_.createElement("appSettings");    doc_.appendChild(root);    QDomElement ip_node = doc_.createElement("ip");    QDomElement port_node = doc_.createElement("port");    QDomText text = doc_.createTextNode(ip);    ip_node.appendChild(text);    text = doc_.createTextNode(port);    port_node.appendChild(text);    root.appendChild(ip_node);    root.appendChild(port_node);    QTextStream out(&file);    doc_.save(out, 4);    return true;}bool XmlHelper::AddXmlNode(QDomElement& parent, const QString &node_tag, const QString &text){    if (!LoadXmlFile())    {        return false;    }    QDomNodeList node_list = doc_.elementsByTagName(parent.tagName());    if (node_list.count() > 0)    {        QDomElement elem = doc_.createElement(node_tag);        QDomText elem_text = doc_.createTextNode(text);        elem.appendChild(elem_text);        parent.appendChild(elem);        QFile file(file_name_);        if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))        {            return false;        }        QTextStream out(&file);        doc_.save(out, 4);        file.close();        return true;    }    return false;}bool XmlHelper::AddXmlNode(const QString &parent_tag, const QString &node_tag, const QString &text){    if (!LoadXmlFile())    {        return false;    }    QDomNodeList node_list = doc_.elementsByTagName(parent_tag);int count = node_list.count();    if (count > 0)    {        QDomElement elem = doc_.createElement(node_tag);        QDomText elem_text = doc_.createTextNode(text);        elem.appendChild(elem_text);        node_list.at(count - 1).appendChild(elem);        QFile file(file_name_);        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))        {            return false;        }        QTextStream out(&file);        doc_.save(out, 4);        file.close();        return true;    }    return false;}bool XmlHelper::RemoveXmlNode(const QString &node_tag){    if (!LoadXmlFile())    {        return false;    }    QDomNodeList node_list = doc_.elementsByTagName(node_tag);    if (node_list.count() > 0)    {        QDomNode parent(node_list.at(0).parentNode());        parent.removeChild(node_list.at(0));        QFile file(file_name_);        if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))        {            return false;        }        QTextStream out(&file);        doc_.save(out, 4);        file.close();        return true;    }    return false;}bool XmlHelper::ParseXmlDocument(const QString &file_name){    file_name_ = file_name;    if (!LoadXmlFile())    {        return false;    }    QDomElement doc_elem = doc_.documentElement();    QDomNode node = doc_elem.firstChild();    while (!node.isNull())    {        QDomElement elem = node.toElement();        if (!elem.isNull())        {            qDebug() << elem.tagName() << ": " << elem.text()                        << endl;        }        node = node.nextSibling();    }    doc_.clear();    return true;}QString XmlHelper::ParseXmlNodeContent(const QString &node_tag){    if (!LoadXmlFile())    {        return QString();    }    QDomNodeList node_list = doc_.elementsByTagName(node_tag);    if (node_list.count() > 0)    {        if (!node_list.at(0).firstChild().isText())        {            return QString();        }        return node_list.at(0).toElement().text();    }    return QString();}QStringList XmlHelper::ParseXmlNodeList(const QString &node_tag){    QStringList str_list;    if (!LoadXmlFile())    {        return str_list;    }    QDomNodeList node_list = doc_.elementsByTagName(node_tag);    for (int i = 0; i < node_list.count(); i++)    {        if (node_list.at(i).firstChild().isText())        {            str_list.append(node_list.at(i).toElement().text());        }        else        {            str_list.append(" ");        }    }    return str_list;}QString XmlHelper::ParseXmlNodeContent(const QDomElement &elem){    if (!LoadXmlFile())    {        return QString();    }    QDomElement root = doc_.documentElement();    QDomNode node = root.firstChild();    while (!node.isNull())    {        if (node.toElement().tagName() == elem.tagName())        {            return elem.text();        }        node = node.nextSibling();    }    return QString("");}QString XmlHelper::ParseXmlNodeContent(const QDomNode &special_node){    if (!LoadXmlFile())    {        return QString();    }    QDomElement root = doc_.documentElement();    QDomNode node = root.firstChild();    while (!node.isNull())    {        if (node.toElement().tagName() == special_node.toElement().tagName())        {            return special_node.toElement().text();        }        node = node.nextSibling();    }    return QString("");}QString XmlHelper::ParseXmlNodeAttribute(const QString &node_tag, const QString& attr_key){    if (!LoadXmlFile())    {        return QString();    }    QString str;    QDomNodeList node_list = doc_.elementsByTagName(node_tag);    if (node_list.count() > 0)    {        QDomElement elem = node_list.at(0).toElement();        str = elem.attributeNode(attr_key).toText().data();    }    return str;}QString XmlHelper::ParseXmlNodeAttribute(const QDomElement &elem, const QString& attr_key){    if (!LoadXmlFile())    {        return QString();    }    QString str;    QDomNodeList node_list = doc_.elementsByTagName(elem.tagName());    if (node_list.count() > 0)    {        QDomElement elem = node_list.at(0).toElement();        str = elem.attributeNode(attr_key).toText().data();    }    return str;}bool XmlHelper::SetXmlNodeContent(const QDomElement &elem, const QString&){    if (!LoadXmlFile())    {        return false;    }    QDomElement root = doc_.documentElement();    QDomNode node = root.firstChild();    while (!node.isNull())    {        if (node.toElement().tagName() == elem.tagName())        {            doc_.replaceChild(elem, node);            return true;        }        node = node.nextSibling();    }    return false;}bool XmlHelper::SetXmlNodeContent(const QString &tag_name, const QString &text){    if (!LoadXmlFile())    {        return false;    }    QDomElement root = doc_.documentElement();    QDomNodeList node_list = root.elementsByTagName(tag_name);    if (node_list.count() > 0)    {        QDomElement elems = node_list.at(0).toElement();        QDomNode old_node = elems.firstChild();        if (elems.firstChild().isText())        {            elems.firstChild().setNodeValue(text);            QDomNode new_node = elems.firstChild();            elems.replaceChild(new_node, old_node);        }        else        {            elems.firstChild().setNodeValue(text);            QDomText text_node;            text_node.setNodeValue(text);            QDomNode new_node = elems.firstChild();            new_node.appendChild(text_node);            elems.replaceChild(new_node, old_node);        }        QFile file(file_name_);        if (!file.open(QIODevice::WriteOnly))        {            return false;        }        QTextStream out(&file);        doc_.save(out, 4);        file.close();        return true;    }    return false;}bool XmlHelper::SetXmlNodeAttribute(const QString& node_tag, const QString &attr_key, const QString &attr_value){    if (!LoadXmlFile())    {        return false;    }    QDomNodeList node_list = doc_.elementsByTagName(node_tag);    if (node_list.count() > 0)    {        QDomElement elem = node_list.at(0).toElement();        elem.setAttribute(attr_key, attr_value);        QFile file(file_name_);        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))        {            return false;        }        QTextStream out(&file);        doc_.save(out, 4);        file.close();        return true;    }    return false;}QDomElement XmlHelper::GetXmlRootNode(){    if (!LoadXmlFile())    {        return QDomElement();    }    return doc_.firstChild().toElement();}QDomElement XmlHelper::GetXmlNodeParent(const QString &node_tag){    if (!LoadXmlFile())    {        return QDomElement();    }    QDomNodeList node_list = doc_.elementsByTagName(node_tag);    if (node_list.count() > 0)    {        return node_list.at(0).toElement();    }    return QDomElement();}bool XmlHelper::ClearXmlDocument(){    if (!LoadXmlFile())    {        return false;    }    QFile file(file_name_);    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))    {        return false;    }    QTextStream out(&file);    doc_.clear();    doc_.save(out, 4);    return true;}bool XmlHelper::AddRootNode(const QString &root_node){    QFile file(file_name_);    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))    {        return true;    }    QDomProcessingInstruction instruction =            doc_.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");    doc_.appendChild(instruction);    QDomElement root = doc_.createElement(root_node);    doc_.appendChild(root);    QTextStream out(&file);    doc_.save(out, 4);    file.close();    return true;}bool XmlHelper::LoadXmlFile(){    doc_.clear();    QFile file(file_name_);    if (!file.open(QIODevice::ReadWrite))    {        file.close();        return false;    }    QTextStream out(&file);    out.setCodec("UTF-8");    QString xml_str(out.readAll().toUtf8());    if (!doc_.setContent(xml_str))    {        file.close();        return false;    }    file.close();    return true;}


使用示例:


XmlHelper *helper = XmlHelper::GetInstance();
xmlhelper_->CreateXmlDocument("test.xml", SignalerElement::SignalerList);xmlhelper_->AddXmlNode(SignalerElement::SignalerList, SignalerElement::Signaler);xmlhelper_->AddXmlNode(SignalerElement::Signaler, SignalerElement::Name, signaler.signaler_name);





原创粉丝点击