QT XML文件的读写

我决定在这写下自己每天获得的知识,有空可以当作翻翻的依据。内容尽管很杂。

SVG是一种用XML定义的语言,用来描述二维矢量及矢量/栅格图形。


enum QIODevice::OpenModeFlag
flags QIODevice::OpenMode

This enum is used with open() to describe the mode in which a device is opened. It is also returned by openMode().

Constant Value Description
QIODevice::NotOpen 0x0000 The device is not open.
QIODevice::ReadOnly 0x0001 The device is open for reading.
QIODevice::WriteOnly 0x0002 The device is open for writing.
QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.
QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed.
QIODevice::Truncate  ;f覆盖了原来的内容;

truncated

[英] [ˈtrʌŋkeitid] [美] [ˈtrʌŋˌketɪd] 生词本
  • 简明释义

  • 更多资料

adj.

v.

bypassed

  • v. 绕道,忽视(bypass的过去分词)


源码:写xml文件

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QTextStream>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QFile *file=new QFile("/home/qust/qt/XML/1.xml");
    if(!file->open(QIODevice::WriteOnly|QIODevice::ReadOnly))
    {
        return ;
    }
    QDomDocument doc;
    QDomText text;
    QDomElement element;
    QDomProcessingInstruction instruction;
    instruction=doc.createProcessingInstruction("xml","version=\'1.0\'");
    doc.appendChild(instruction);
    QDomElement root=doc.createElement("kdevelop");
    doc.appendChild(root);
    QDomElement general=doc.createElement("general");
    root.appendChild(general);

    element=doc.createElement("author");
    text=doc.createTextNode("zeki");

    element.appendChild(text);
    general.appendChild(element);

    element=doc.createElement("email");
    text=doc.createTextNode("caizhiming@tom.com");
    element.appendChild(text);
    general.appendChild(element);

    QTextStream out(file);
    doc.save(out,4);
    file->close();

}

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


运行后:










读xml源码:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtXml>
#include <QDebug>
#include <QFile>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QDomDocument dom;
    QFile *file=new QFile("/home/qust/qt/XML/1.xml");
    if(file->open(QIODevice::ReadOnly))
    {
        dom.setContent(file);

    }

    QDomNodeList email=dom.elementsByTagName("email");
    qDebug()<<email.count();
    qDebug()<<email.item(0).toElement().text();
    // QDomNodeList general=kdevelop.item(0).toElement().childNodes();
    //qDebug()<<general.item(0).toElement().text();
}

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值