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();

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);  //添加根元素

linux

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);

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

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);

#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();
}

相关文章
相关标签/搜索