Qt之Doc创建xml

 

    QFile file("my.xml");//建立指向my.xml文件的QFile对象.
    if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) // 只写方式打开,并清空以前的信息
    {
        qDebug() << "打开失败";
        return 0;
    }

    
    QDomDocument doc;  //新建立一个QDomDocument对象,表示一个xml文档
    QDomProcessingInstruction instrution;  //添加处理指令
    instrution = doc.createProcessingInstruction("xml",
                                                 "version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instrution);

    
    QTextStream out(&file);
    doc.save(out, 4);  //将文档保存到文件,4为子元素缩进字符数
    file.close();

11cb4928dfab0407af60e04bfb50ab01faa.jpg

    QDomDocument doc;  //新建立一个QDomDocument对象,表示一个xml文档
    QDomProcessingInstruction instrution;  //添加处理指令
    instrution = doc.createProcessingInstruction("xml",
                                                 "version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instrution);
    QDomElement root = doc.createElement("书库");
    doc.appendChild(root);  //添加根元素

c7755bcf4460d84886b427d3df3e2f681f6.jpg

    QDomDocument doc;  //新建立一个QDomDocument对象,表示一个xml文档
    QDomProcessingInstruction instrution;  //添加处理指令
    instrution = doc.createProcessingInstruction("xml",
                                                 "version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instrution);
        QDomElement root = doc.createElement("书库");
    doc.appendChild(root);  //添加根元素
            QDomElement book = doc.createElement("图书");
            QDomAttr id = doc.createAttribute("编号");
        root.appendChild(book);

9fd508e485c1a954d043023fa65eb76179f.jpg

    doc.appendChild(instrution);
        QDomElement root = doc.createElement("书库");
    doc.appendChild(root);  //添加根元素
            QDomElement book = doc.createElement("图书");
                QDomAttr id = doc.createAttribute("编号");
            book.setAttributeNode(id);
        root.appendChild(book);

fa22fd2fa6ec337b63921854d5113074a69.jpg

    QDomDocument doc;  //新建立一个QDomDocument对象,表示一个xml文档
    QDomProcessingInstruction instrution;  //添加处理指令
    instrution = doc.createProcessingInstruction("xml",
                                                 "version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instrution);
        QDomElement root = doc.createElement("书库");
    doc.appendChild(root);  //添加根元素
            QDomElement book = doc.createElement("图书");
                QDomAttr id = doc.createAttribute("编号");
                id.setValue("1");
            book.setAttributeNode(id);
        root.appendChild(book);

a94012ed89431cba71bf0e4c26a275d93ac.jpg

#include <QCoreApplication>
#include <QtXml>
#include <QDebug>
/*
 * 使用QDomDocument在内存种生成一颗Dom树,然后调用save()函数利用QTextStream文本流将Dom树保存在了文件中,
 * 在生成Dom树时主要使用了createElement()等函数来生成各种节点,然后使用appendChild()将各个节点一次追加进去
*/

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile file("my.xml");//建立指向my.xml文件的QFile对象.
    if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) // 只写方式打开,并清空以前的信息
    {
        qDebug() << "打开失败";
        return 0;
    }


    QDomDocument doc;  //新建立一个QDomDocument对象,表示一个xml文档
    QDomProcessingInstruction instrution;  //添加处理指令
    instrution = doc.createProcessingInstruction("xml",
                                                 "version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instrution);
        QDomElement root = doc.createElement("书库");
    doc.appendChild(root);  //添加根元素
            QDomElement book = doc.createElement("图书");
                QDomAttr id = doc.createAttribute("编号");
                id.setValue("1");
            book.setAttributeNode(id);  //图书可以有自己的属性
                QDomElement title = doc.createElement("书名");
                    QDomText text = doc.createTextNode("Qt");
                title.appendChild(text);
            book.appendChild(title);
                QDomElement author = doc.createElement("作者");
                             text = doc.createTextNode("shiming");//文本节点
                author.appendChild(text);  //QT&shuiming是用来描述书名和作者的,因此是孩子
            book.appendChild(author); //书名&作者是图书的孩子,他们是用来描述图书的
        root.appendChild(book);

            //添加第2个节点
            book = doc.createElement("图书");
                id = doc.createAttribute("编号");
                id.setValue("2");
            book.setAttributeNode(id);  //图书可以有自己的属性
                title = doc.createElement("书名");
                    text = doc.createTextNode("linux");
                title.appendChild(text);
            book.appendChild(title);
                author = doc.createElement("作者");
                     text = doc.createTextNode("yafei");//文本节点
                author.appendChild(text);  //QT&shuiming是用来描述书名和作者的,因此是孩子
            book.appendChild(author); //书名&作者是图书的孩子,他们是用来描述图书的
        root.appendChild(book);

    QTextStream out(&file);
    doc.save(out, 4);  //将文档保存到文件,4为子元素缩进字符数
    file.close();


    return a.exec();
}

2141b74019ee3f71c45faa06133a35e73c4.jpg

转载于:https://my.oschina.net/u/3919756/blog/2051308

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值