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();
        }
    }
}
相关推荐
利刃大大2 小时前
【在线五子棋对战】二、websocket && 服务器搭建
服务器·c++·websocket·网络协议·项目
喜欢吃燃面2 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked932 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
虚拟之3 小时前
36、stringstream
c++
我很好我还能学3 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
南岩亦凛汀4 小时前
在Linux下使用wxWidgets进行跨平台GUI开发
c++·跨平台·gui·开源框架·工程实战教程
曦月逸霜5 小时前
第34次CCF-CSP认证真题解析(目标300分做法)
数据结构·c++·算法
galaxy_strive5 小时前
绘制饼图详细过程
开发语言·c++·qt
Unpredictable2226 小时前
【VINS-Mono算法深度解析:边缘化策略、初始化与关键技术】
c++·笔记·算法·ubuntu·计算机视觉
PingdiGuo_guo7 小时前
C++智能指针的知识!
开发语言·c++