• 39XML文档类


    Xml源代码

    domxml.h

    #ifndef DOMXML_H

    #define DOMXML_H

    #include <QString>

    #include <QStringList>

    #include <QDomDocument> //文件

    #include <QDomProcessingInstruction>    //头部

    #include <QDomElement>  //元素

    #include <QDomText>

    class DomXML

    {

    public:

        DomXML();

        //创建空的XML

        static void CreateXML(QString strFilePath);

        //搭建XML结构

        static void AppendXML(QString strFilePath,QStringList list);

        //写XML

        static void WriteXML(QDomDocument &doc,QDomElement &root,QStringList list);

        //写XML

        static void ReadXML(QString strFilePath,

                             QStringList &fList,

                             QStringList &bList,

                             QStringList &pList,

                             QStringList &nList,

                             QStringList &tList

                             );

    };

    #endif // DOMXML_H

    domxml.cpp

    #include "domxml.h"

    #include <QFile>

    #include <QTextStream>

    #include <QDateTime>

    #include <QDebug>

    #define cout qDebug()<<"["<<__FILE__<<":"<<__LINE__<<"]"

    DomXML::DomXML()

    {

    }

    //创建空的xml文件

    void DomXML::CreateXML(QString strFilePath)

    {

        //打开文件

        QFile file(strFilePath);

        if(file.exists())

        {

            cout<<"FilePath exists";

            return ;

        }

        else

        {

            bool bIsOk=file.open(QIODevice::WriteOnly);

            if(bIsOk)

            {//打开文件成功

                //创建xml文档对象

                QDomDocument doc;

                //创建xml头部格式<?xml version="1.0" encoding="UTF-8"?>

                QDomProcessingInstruction ins;

                ins=doc.createProcessingInstruction("xml","version="1.0" encoding="UTF-8"");

                //追加元素

                doc.appendChild(ins);

                //根节点元素

                QDomElement root=doc.createElement("日期销售清单");

                doc.appendChild(root);

                //保存

                QTextStream stream(&file);//文本流关联文件

                doc.save(stream,4); //4代表缩进4个字符

            }

            else

            {//文件打开失败

                cout<<"open file error";

                return ;

            }

        }

            file.close();

    }

    //搭建XML格式

    void DomXML::AppendXML(QString strFilePath,QStringList list)

    {

        //打开文件

        QFile file(strFilePath);

        bool bIsOk=file.open(QIODevice::ReadOnly);

        if(bIsOk)

        {

            //file和xml文档对象关联

            QDomDocument doc;

            bIsOk=doc.setContent(&file);

            if(bIsOk)

            {//关联成功

                file.close();

                //根节点元元素

                QDomElement root=doc.documentElement();

                //获取当地时间

                QDateTime date=QDateTime::currentDateTime();

                QString strDate=date.toString("yyyy-MM-dd");//2018-2-8

                //qDebug()<<strDate;

                //判断根节点下有没有子节点

                if(root.hasChildNodes())

                {

                    //查找最后一个子节点

                    QDomElement lastEmt=root.lastChildElement();

                    //日期相同,同一天不创建新节点

                    if(lastEmt.attribute("date")==strDate)

                    {//相同直接写入

                         WriteXML(doc,lastEmt,list);

                    }

                    else

                    {//不相同创建新节点

                        //创建日期子节点元素

                        QDomElement dateEmt=doc.createElement("日期");

                        //创建date属性

                        QDomAttr dateAttr=doc.createAttribute("date");

                        //设置属性的值

                        dateAttr.setNodeValue(strDate);

                        //节点和属性关联

                        dateEmt.setAttributeNode(dateAttr);

                        //把日期节点追加到根节点

                        root.appendChild(dateEmt);

                        WriteXML(doc,dateEmt,list);

                    }

                }

                else

                {//没有子节点

                    //创建日期子节点元素

                    QDomElement dateEmt=doc.createElement("日期");

                    //创建date属性

                    QDomAttr dateAttr=doc.createAttribute("date");

                    //设置属性的值

                    dateAttr.setNodeValue(strDate);

                    //节点和属性关联

                    dateEmt.setAttributeNode(dateAttr);

                    //把日期节点追加到根节点

                    root.appendChild(dateEmt);

                    WriteXML(doc,dateEmt,list);

                }

                bIsOk=file.open(QIODevice::WriteOnly);

                if(bIsOk)

                {

                    //保存

                    QTextStream stream(&file);//文本流关联文件

                    doc.save(stream,4); //4代表缩进4个字符

                }

                file.close();

            }

            else

            {

                cout<<"setContent error";

                return ;

            }

        }

        else

        {

            cout<<"open file error";

            return ;

        }

    }

    //写XML

    void DomXML::WriteXML(QDomDocument &doc,QDomElement &root,QStringList list)

    {

        //获取当地时间

        QDateTime time=QDateTime::currentDateTime();

        QString strTime=time.toString("hh-mm-ss");//20:20:20

        //创建时间节点元素

        QDomElement timeEmt=doc.createElement("时间");

        //创建元素属性

        QDomAttr timeAttr=doc.createAttribute("time");

        //设置属性的值

        timeAttr.setNodeValue(strTime);

        //时间节点与属性相关联

        timeEmt.setAttributeNode(timeAttr);

        //将时间节点追加到日期节点后

        root.appendChild(timeEmt);

        QDomElement factory=doc.createElement("厂家");

        QDomElement brand=doc.createElement("品牌");

        QDomElement price=doc.createElement("价格");

        QDomElement num=doc.createElement("数量");

        QDomElement total=doc.createElement("金额");

        //设置文本内容并追加到前节点元素

        QDomText text=doc.createTextNode(list.at(0));

        factory.appendChild(text);

        text=doc.createTextNode(list.at(1));

        brand.appendChild(text);

        text=doc.createTextNode(list.at(2));

        price.appendChild(text);

        text=doc.createTextNode(list.at(3));

        num.appendChild(text);

        text=doc.createTextNode(list.at(4));

        total.appendChild(text);

        //将厂家、价格等节点追加到哦时间节点

        timeEmt.appendChild(factory);

        timeEmt.appendChild(brand);

        timeEmt.appendChild(price);

        timeEmt.appendChild(num);

        timeEmt.appendChild(total);

    }

    //读XML

    void DomXML::ReadXML(QString strFilePath,QStringList &fList,QStringList &bList,QStringList &pList,QStringList &nList,QStringList &tList)

    {

        QFile file(strFilePath);

        bool bIsOk=file.open(QIODevice::ReadOnly);

        QDateTime date=QDateTime::currentDateTime();

        QString strDate=date.toString("yyyy-MM-dd");

        if(bIsOk)

        {

            QDomDocument doc;

            bIsOk=doc.setContent(&file);

            if(bIsOk)

            {

                QDomElement root=doc.documentElement();

                //是否有子节点

                if(root.hasChildNodes())

                {

                   QDomElement lastEmt=root.lastChildElement();

                   if(lastEmt.attribute("date")==strDate)

                   {//找到当天日期下所有时间子节点

                       QDomNodeList list=lastEmt.childNodes();

                       for(int i=0;i<list.size();++i)

                       {

                           QDomNodeList subList=list.at(i).toElement().childNodes();

                           //厂家

                           QString factory=subList.at(0).toElement().text();

                           fList.append(factory);

                           //品牌

                           QString brand=subList.at(1).toElement().text();

                           bList.append(brand);

                           //价格

                           QString price=subList.at(2).toElement().text();

                           pList.append(price);

                           //数量

                           QString num=subList.at(3).toElement().text();

                           nList.append(num);

                           //金额

                           QString total=subList.at(4).toElement().text();

                           tList.append(total);

                       }

                   }

                   else

                   {

                       qDebug()<<"没有当天日期";

                       return ;

                   }

                }

            }

        }

        else

        {

           cout<<"open file error";

        }

    }

    测试调用部分

    mainwindow.cpp

    #include "mainwindow.h"

    #include "ui_mainwindow.h"

    #include <QStringList>

    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :

        QMainWindow(parent),

        ui(new Ui::MainWindow)

    {

        ui->setupUi(this);

        QStringList list;

        list<<"二汽神龙"<<"毕加索"<<"39"<<"1"<<"20";

        DomXML::CreateXML("../demo.xml");

        DomXML::AppendXML("../demo.xml",list);

        QStringList fList;

        QStringList bList;

        QStringList pList;

        QStringList nList;

        QStringList tList;

        DomXML::ReadXML("../demo.xml",fList,bList,pList,nList,tList);

        for(int i=0;i<fList.size();++i)

        {

            QString str=QString("%1:%2:卖出了%3,单价:%4总价:%5")

                        .arg(fList.at(i))

                    .arg(bList.at(i))

                    .arg(nList.at(i))

                    .arg(pList.at(i))

                    .arg(tList.at(i));

            qDebug()<<str.toUtf8().data();

        }

    }

    MainWindow::~MainWindow()

    {

        delete ui;

    }

     

  • 相关阅读:
    gif&png&jpg&webp
    设计点滴&css效果点滴
    backbone点滴
    js自己总结的小东西(打印出来方便学习)
    nodejs点滴
    js类型
    mongo学亮的分享
    npm package.json中的dependencies和devDependencies的区别
    161130、Dubbo+SpringMVC工程创建详解
    161129、详解5种跨域方式及其原理
  • 原文地址:https://www.cnblogs.com/gd-luojialin/p/9215897.html
Copyright © 2020-2023  润新知