zoukankan      html  css  js  c++  java
  • QT 读取Xml文件

    1、Xml文件内容

    <?xml version="1.0" encoding="UTF-8"?>
    <PanelType>
        <LeftPanel>
            <Type Text="工程名称(*)"/>
            <Type Text="工程编号(*)"/>
            <Type Text="录入人(*)"/>
            <Type Text="工程负责人(*)"/>
            <Type Text="创建时间(*)"/>123</LeftPanel>
        <RightPanel>
            <Type Edit="2" Field="PROJECTNAME" alias="" default="" readonly="0" sql="" length="200"/>
            <Type Edit="2" Field="PROJECTID" alias="" default="" readonly="0" sql="" length="50"/>
            <Type Edit="2" Field="ENTERONE" alias="" default="" readonly="1" sql="" length="200"/>
            <Type Edit="2" Field="PERSONNAME" alias="" default="" readonly="0" sql="" length="200"/>
            <Type Edit="2" Field="CREATETIME" alias="" default="sysdate" readonly="1" sql="" length=""/>
        </RightPanel>
        <ButtonPanel>
            <Type Button="保存" hide="0"/>
            <Type Button="取消" hide="0"/>
        </ButtonPanel>
    </PanelType>

    2、具体实现代码main.cpp

    #include <QApplication>
    #include <QDomDocument>
    #include <QFile>
    #include <QByteArray>
    #include <QDebug>
    #include <QDomNodeList>
    #include <QDomNode>
    #include <QIODevice>
    #include <QMessageBox>
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QFile file("E:/QT_STUDY_HEIMA/data_test.xml");
      // if( file.open(QFile::ReadOnly  == false)) return -1;
       // QByteArray datas = file.readAll();
        //file.close();
        if (!file.open(QFile::ReadOnly | QFile::Text))
           {
    
               return false;
           }
    
    
        QDomDocument document;
       if(document.setContent(file.readAll()) == false) return -1;
       QDomElement qDomElement = document.documentElement();//返回根节点
        QDomNodeList roots = document.childNodes();//获得孩子节点
        //如果节点是元素或者节点  两种数据结构 元素转节点 domNode.toElement()
        //foreach(QDomNode root,roots)
        // foreach 循环直接读取的值是最外层的节点  也就是与 根节点是同级的节点
        for (int iRoot = 0;iRoot < roots.count();iRoot ++)
        {
            QDomNode root = roots.at(iRoot);
            qDebug()<< root.nodeName();
            if(root.isElement()){
                QDomNodeList childs = root.childNodes();//根节点内部的  相同级的节点
               // for (QDomNode child : childs)
                for (int iChild =0 ;iChild < childs.count();iChild ++)
    
                {
                    QDomNode child = childs.at(iChild);
                   qDebug()<< child.nodeName();
                   if(child.hasAttributes())
                   {
                      QDomNamedNodeMap map = child.attributes();
                      int nCount = map.count();
                      for (int i = 0;i<nCount;i++) {
                       QDomNode node = map.item(i);
                       qDebug()<<node.nodeName() <<"="<<node.nodeValue();
    
                      }
                   }
                   QDomNode qDomNodeName = child;
                   if(qDomNodeName.childNodes().count() > 0)
                   {
                       //qDebug()<< root.firstChild().nodeValue();
                       QDomNode qDomNodeName = root.childNodes().at(0);
                       QDomNode qDomNode = qDomNodeName.firstChild();
                       qDebug()<<"ceshi"<< qDomNode.nodeName()<<":"<<qDomNode.toElement().attribute("Text");
    
                   }
                }
            }else {
    
               // qDebug()<< root.firstChild().nodeValue();
    
    
            }
    
    
        }
    
        return a.exec();
    }

    3、实现效果

    说明:程序在读取最后一层节点的时候,输出的内容还是有些问题的,有待改进,但基本上实现了一层一层读取xml标签的内容

    参考学习链接:

    https://www.cnblogs.com/herd/p/11897680.html

    https://blog.csdn.net/lvdepeng123/article/details/85242914

  • 相关阅读:
    centos7.9安装mysql,远程无法连接的问题
    netcore 自定义脚手架
    mongodb查询出某个字段最大值
    解决Docker容器内不能使用vim命令的问题
    git 撤销修改和版本回退
    【转】一文读懂PCA算法的数学原理
    【转】Maximal Information Coefficient (MIC)最大互信息系数详解与实现
    【转】带约束的多目标优化进化算法综述
    论文快报-2021-10-Multi-task optimization and evolutionary multitasking
    【Vegas原创】SQL Server数据库备份、差异备份、日志备份脚本
  • 原文地址:https://www.cnblogs.com/gjianli/p/15046490.html
Copyright © 2011-2022 走看看