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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM