qt_c++_xml简单示范demo

迅雷链接

链接:https://pan.xunlei.com/s/VO8bJODxPfPHE0x3nfUa2KZ1A1?pwd=tuxq#

复制这段内容后打开手机迅雷App,查看更方便

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextCodec>


#include <QFile>
#include <QDomDocument>
#include <QTextStream>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE


void createXML(QString filePath);
void loadXml(QString xmlName);

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QDebug>

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



    QString path="C:/Users/36075/Desktop/QtC_xml/test.xml";
    createXML(path);
    loadXml(path);
}

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


void createXML(QString filePath)
{

           QFile file(filePath);

               bool isOk = file.open(QIODevice::WriteOnly);//以只写方式打开文件 isOk用来判断是否打开成功
         //      打开成功的话:
        //       第一步:创建XML文档对象,包含头文件#include <QDomDocument>  //文件
                 QDomDocument doc;
                 QDomProcessingInstruction instruction; //添加处理命令
                     instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
                     doc.appendChild(instruction);

                 QDomElement root = doc.createElement("bookstore");
                     doc.appendChild(root);

                             QDomElement book = doc.createElement("book");
                             book.setAttribute("category", "C++");	//生成category节点
                             root.appendChild(book);

                             QDomElement price= doc.createElement("price");
                             book.appendChild(price);
                             QDomText text = doc.createTextNode("98");
                             price.appendChild(text);

                             book = doc.createElement("book");
                             book.setAttribute("category", QString::fromLocal8Bit("语文"));
                             root.appendChild(book);

                             price= doc.createElement("price");
                                 text = doc.createTextNode("100");
                                 price.appendChild(text);
                             book.appendChild(price);


                 QTextStream stream(&file);
                     stream.setCodec("UTF_8");
                     doc.save(stream,4,QDomNode::EncodingFromTextStream);
                     file.close();

}


void loadXml(QString xmlName)
{
    QFile file(xmlName);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        return;
    }

    QString strError;
    int errorLine;
    int errorColumn;
    QDomDocument doc;
    if(!doc.setContent(&file, false, &strError, &errorLine, &errorColumn)){
        return;
    }
    QDomElement root = doc.documentElement();
    if(root.tagName() == "bookstore")
    {
        QDomNode book = root.firstChild();
        while(!book.isNull())
        {
            if(book.toElement().tagName() == "book")
            {
                QString str = book.toElement().attribute("category");	//获取category属性内容
                qDebug()<<str;
                QDomNode node = book.firstChild();
                while(!node.isNull())
                {
                    if(node.toElement().tagName() == "price")
                    {
                        QString price = node.toElement().text();
                        qDebug()<<price;
                    }
                    node = node.nextSibling();
                }
            }
            book = book.nextSibling();
        }
    }
}
相关推荐
reverie.Ly18 分钟前
从0开始linux(12)——命令行参数与环境变量
android·linux·c++
Antonio91521 分钟前
【Codeforces】CF 2005 C
开发语言·c++·算法
月夕花晨3741 小时前
C++学习笔记(51)
c++·笔记·学习
小羊在奋斗1 小时前
【C++】二叉搜索树+变身 = AVL树
java·开发语言·c++·机器学习
三玖诶2 小时前
第三弹:C++ 中的友元机制与运算符重载详解
开发语言·c++·运算符重载·友元
管家罢了3 小时前
C++-容器适配器- stack、queue、priority_queue和仿函数
开发语言·数据结构·c++
月夕花晨3748 小时前
C++学习笔记(50)
c++·笔记·学习
Mr_Xuhhh9 小时前
数据结构阶段测试2的一点小补充
android·开发语言·汇编·数据结构·c++·算法
Bartender_Jill10 小时前
[ROS2]解决PyQt5和sip的各种报错问题 stderr: qt_gui_cpp
开发语言·python·qt·机器人·数据可视化
weixin_6320776310 小时前
c++抽象类 abstract class
开发语言·c++·多态