Qt XML文件读、写、修改

1 XML写入

1.1 使用的库

#include "qdom.h"  

1.2 主体语句

头部创建:

    //创建xml文件头部
    QDomDocument doc;
    QDomProcessingInstruction instruction;   //添加处理命令
    instruction = doc.createProcessingInstruction("xml","version\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);
    QDomElement root = doc.createElement("颜色矫正参数");  //根节点
    doc.appendChild(root);

内容创建:

//color值写入
    QString color =ui->color_edit->text();
    QDomElement XMLColor = doc.createElement("Color");
    QDomElement sub_title = doc.createElement("color_1");
    QDomText text; //括号标签中间的值
    text = doc.createTextNode(color);    //创建文本节点
    XMLColor.appendChild(sub_title);   //父子结构构建
    sub_title.appendChild(text);
    root.appendChild(XMLColor);

1.3 详细例子说明

.h文件
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
    class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
   
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

.cpp文件

#include "widget.h"
#include "ui_widget.h"
#include "QFile"
#include "QMessageBox"
#include "qdom.h"       //在pro文件中加入QT +=xml  
#include "QDebug"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
   
    ui->setupUi(this);
    setWindowTitle("颜色矫正参数更改");
}

Widget::~Widget()
{
   
    delete ui;
}


void Widget::on_pushButton_clicked()
{
   
    QString fileName = QCoreApplication::applicationDirPath() + "/data.xml";
    qDebug()<<"path:"<<fileName;
    QFile file(fileName);
    if(!file.open(QFile::WriteOnly|QFile::Truncate))  //重写方式打开
    {
   
        QMessageBox::information(this,tr("错误"),tr("data文件写入失败"));
        return;
    }

    //创建xml文件头部
    QDomDocument doc;
    QDomProcessingInstruction instruction;   //添加处理命令
    instruction = doc.createProcessingInstruction("xml","version\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);
    QDomElement root = doc.createElement("颜色矫正参数");  //根节点
    doc.appendChild(root);

    //color值写入
    QString color =ui->color_edit->text();
    QDomElement XMLColor = doc.createElement("Color");
    QDomElement sub_title = doc.createElement("color_1");
    QDomText text; //括号标签中间的值
    text = doc.createTextNode(color);
    XMLColor.appendChild(sub_title);
    sub_title.appendChild(text);
    root.appendChild(XMLColor);

    //RGB值写入
    QString R_value =ui->R_edit->text();
    QString G_value =ui->G_edit->text();
    QString B_value =ui->B_edit->text();
    QDomElement XMLRGB = doc.createElement("RGB");

    QDomElement RGB_title = doc.createElement("R");
    QDomText RGB_text; //括号标签中间的值
    RGB_text = doc.createTextNode(R_value);
    XMLRGB.appendChild(RGB_title);
    RGB_title.appendChild(RGB_text);
    root.appendChild(XMLRGB);

    RGB_title = doc.createElement("G");
    RGB_text = doc.createTextNode(G_value);
    XMLRGB.appendChild(RGB_title);
    RGB_title.appendChild(RGB_text);
    root.appendChild(XMLRGB);

    RGB_title = doc.createElement("B");
    RGB_text = doc.createTextNode(B_value);
    XMLRGB.appendChild(
  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用QtQtCore和QtXml模块来XML文件。下面是一个简单的示例代码,展示了如何使用Qt读取XML文件XML文件: ```cpp #include <QtXml/QtXml> #include <QtCore/QFile> #include <QtCore/QIODevice> int main() { // 读取XML文件 QFile file("example.xml"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Failed to open XML file for reading."; return -1; } QDomDocument doc; if (!doc.setContent(&file)) { qDebug() << "Failed to parse XML."; file.close(); return -1; } file.close(); // 遍历XML数据 QDomElement root = doc.documentElement(); QDomNodeList nodes = root.childNodes(); for (int i = 0; i < nodes.count(); i++) { QDomNode node = nodes.at(i); if (node.isElement()) { QDomElement element = node.toElement(); qDebug() << "Element name:" << element.tagName(); qDebug() << "Element value:" << element.text(); } } // XML文件 QFile outputFile("output.xml"); if (!outputFile.open(QIODevice::WriteOnly)) { qDebug() << "Failed to open output XML file."; return -1; } QTextStream stream(&outputFile); doc.save(stream, 4); // 缩进为4个空格 outputFile.close(); return 0; } ``` 上述示例代码首先打开一个XML文件进行读取,然后使用`QDomDocument`类解析XML数据。通过遍历`QDomDocument`对象的子节点,可以获得每个元素的标签名和文本值。最后,将解析后的XML数据保存到新的XML文件中。 请注意,上述代码中使用的XML文件名为"example.xml"和"output.xml",你可以根据自己的需要修改这些文件名。此外,你需要在.pro文件中添加`QT += xml`以包含QtXml模块。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值