QT写XML

一:WriteXml
1).创建根节点:QDomElement root = doc.documentElement("rootName " );
2).创建元素节点:QDomElement element = doc.createElement("nodeName");
3).添加元素节点到根节点:root. appendChild(element);
4).创建元素文本:QDomText nodeText=doc.createTextNode("text");
5).添加元素文本到元素节点:element. appendChild(nodeText);
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QDomDocument>

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

    QDomDocument doc;
        QDomProcessingInstruction xmlInstruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"");
        QDomComment comment = doc.createComment(QString::fromLocal8Bit("test222"));
        QDomProcessingInstruction styleInstruction = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"style.css\"");
        doc.appendChild(xmlInstruction);  // 开始文档(XML 声明)
        doc.appendChild(comment);  // 注释
        doc.appendChild(styleInstruction);  // 处理指令
        // 根元素 <Blogs>
        QDomElement root = doc.createElement("Books");  //创建元素节点:createElement();
        root.setAttribute("Version", "1.0");  // 属性
        doc.appendChild(root);     //添加元素节点到根节点: appendChild()

        // 元素 <Blog>
        QDomElement child = doc.createElement("Book");
        root.appendChild(child);

        // 元素 <作者>、<时间>、<个人说明>
        QDomElement author = doc.createElement(QString::fromLocal8Bit("author"));
        QDomElement home = doc.createElement(QString::fromLocal8Bit("time"));
        QDomElement instruction = doc.createElement(QString::fromLocal8Bit("attion"));
        child.appendChild(author);
        child.appendChild(home);
        child.appendChild(instruction);

        // 元素的文本数据
        QDomText authorText = doc.createTextNode(QString::fromLocal8Bit("Vincent"));
        QDomText homeText = doc.createTextNode("2017年4月13日");
        QDomText instructionText = doc.createTextNode(QString::fromLocal8Bit("test111"));
        author.appendChild(authorText);    //添加元素文本到元素节点: appendChild()
        home.appendChild(homeText);
        instruction.appendChild(instructionText);

        // 保存 XML 文件
        QString strFile("Books.xml");
        QFile file(strFile);
        // 只写模式打开文件
        if (file.open(QFile::WriteOnly | QFile::Text))
        {
            QTextStream out(&file);
            doc.save(out, QDomNode::EncodingFromDocument);  //保存修改内容到文件
            file.close();
        }
    return a.exec();
}

输出XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--test222-->
<?xml-stylesheet type="text/css" href="style.css"?>
<Books Version="1.0">
 <Book>
  <author>Vincent</author>
  <time>2017年4月13日</time>
  <attion>test111</attion>
 </Book>
</Books>
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QDomDocument>

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

    //打开或创建文件
      QString fileName("test.xml");
      QFile file(fileName);
      //QIODevice::Truncate表示清空原来的内容
      if(!file.open(QFile::WriteOnly|QFile::Truncate))
          return 0;

      //创建xml文档在内存中
      QDomDocument doc;
      //添加处理命令
      QDomProcessingInstruction instruction;
      instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
      //创建注释
      QDomComment comment;
      comment = doc.createComment(QString::fromLocal8Bit("test1234"));
      QDomProcessingInstruction styleInstruction;
      styleInstruction= doc.createProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"style.css\"");
      doc.appendChild(instruction); //文档开始声明
      doc.appendChild(comment);
      doc.appendChild(styleInstruction);  // 处理指令


      //添加根节点
      QDomElement root=doc.createElement("library");
      root.setAttribute("Version","2.1");
      doc.appendChild(root);
      //添加第一个子节点及其子元素
      QDomElement book=doc.createElement("book");
      //方式一:创建属性  其中键值对的值可以是各种类型
      book.setAttribute("id",1);
      //方式二:创建属性 值必须是字符串
      QDomAttr time=doc.createAttribute("time");
      time.setValue("2013/6/13");
      book.setAttributeNode(time);

      QDomElement title=doc.createElement("title"); //创建子元素
      QDomText text; //设置括号标签中间的值
      text=doc.createTextNode("C++ primer");
      title.appendChild(text);    //添加元素文本到元素节点
       book.appendChild(title);   //添加元素节点到根节点  确定上下级关系
      QDomElement author=doc.createElement("author"); //创建子元素
      text=doc.createTextNode("Stanley Lippman");
      author.appendChild(text);
      book.appendChild(author);
      //添加节点book做为根节点的子节点
      root.appendChild(book);    //添加元素节点到根节点  确定上下级关系

      //添加第二个子节点及其子元素
      book=doc.createElement("book");
      book.setAttribute("id",2);
      time=doc.createAttribute("time");
      time.setValue("2007/5/25");
      book.setAttributeNode(time);
      title=doc.createElement("title");
      text=doc.createTextNode("Thinking in Java");
      book.appendChild(title);
      title.appendChild(text);

      author=doc.createElement("author");
      text=doc.createTextNode("Bruce Eckel");
      author.appendChild(text);
      book.appendChild(author);
      root.appendChild(book);

      //输出到文件
      QTextStream out_stream(&file);
      doc.save(out_stream,4); //缩进4格
      file.close();
    return a.exec();
}

输出

<?xml version="1.0" encoding="UTF-8"?>
<!--test1234-->
<?xml-stylesheet type="text/css" href="style.css"?>
<library Version="2.1">
    <book time="2013/6/13" id="1">
        <title>C++ primer</title>
        <author>Stanley Lippman</author>
    </book>
    <book time="2007/5/25" id="2">
        <title>Thinking in Java</title>
        <author>Bruce Eckel</author>
    </book>
</library>

猜你喜欢

转载自blog.csdn.net/weixin_43542624/article/details/85290146