QT XML文件的生成与解析之——QDomDocument

1.在.pro文件里加上QT += xml
2. 读写文件

'1.0' encoding='UTF-8'?>

  "中国" continent="亚洲">
    "广东" referredName="粤" >
	   "1" name="广州"  population="9000" />
	   "2" name="深圳"  population="8000" />
	
	 "广西" referredName="桂" >
	   "1" name="南宁"  population="600" />
	   "2" name="桂林"  population="500" />
	
  

#ifndef CFILEDATA_PRASEXML_H
#define CFILEDATA_PRASEXML_H
#include 
#include 
#include 
#include 
#include 
#include 

class Zone
{
public:
    int nId;
    QString sName;
    QString population;
};

class Province
{
public:
    Province(){
       sName = "";
       sReferredName = "";
       m_listZone.clear();
    }
    ~Province(){
        qDeleteAll(m_listZone);
        m_listZone.clear();
    }
public:
  QString        sName;
  QString        sReferredName;
  QList   m_plistZone;
};

class CFileData_PraseXml
{
public:
    CFileData_PraseXml();
    ~CFileData_PraseXml();
public:
    bool  ParseFileXml();
    bool  generateFileXml(QByteArray&arr);
private:
    QString sName;
    QString sContinent;
    QList  m_plistProvince;
};

#endif // CFILEDATA_PRASEXML_H
#include "cfiledata_prasexml.h"

CFileData_PraseXml::CFileData_PraseXml()
{

}

CFileData_PraseXml::~CFileData_PraseXml()
{
    qDeleteAll(m_plistProvince);
    m_plistProvince.clear();
}

bool CFileData_PraseXml::ParseFileXml()
{
   QString sFileName = QFileDialog::getOpenFileNames(this,QString("打开文件"),
                                                     "/home/","xml files(*.xml)");

   QFile file(sFileName);
   if(!file.open(QIODevice::ReadOnly |QIODevice::Truncate )){
       return false;
   }

   QByteArray arr = file.readAll();
   QDomDocument dom;
   if(!dom.setContent(arr)){
       return false;
   }
   QDomElement rootEle = dom.documentElement();
   if(rootEle.isNull()){
       return false;
   }

   QDomElement countryListEle = rootEle.firstChildElement("countryList");
   if(countryListEle.isNull()){
       return false;
   }
   qDeleteAll(m_plistProvince);
   m_plistProvince.clear();

   sName = countryListEle.attribute("name");
   sContinent = countryListEle.attribute("continent");

   QDomElement privanceEle = countryListEle.firstChildElement("Privance");
   while(!privanceEle.isNull()){
       Province* pNewProvince = new Province;
       pNewProvince->sName = privanceEle.attribute("name");
       pNewProvince->sReferredNamereferredName = privanceEle.attribute("referredName");
       this->m_plistProvince.append(pNewProvince);

       QDomElement zoneEle = privanceEle.firstChildElement("zone");
       while(!zoneEle.isNull()){
           Zone* pZone = new Zone;
           pZone->nId = zoneEle.attribute("id").toInt();
           pZone->sName = zoneEle.attribute("zone");
           pZone->population = zoneEle.attribute("population");
           pNewProvince->m_plistZone.append(pZone);
           zoneEle = zoneEle.nextSiblingElement("zone");
       }
       privanceEle = privanceEle.nextSiblingElement("Privance");
   }
   return true;
}

bool CFileData_PraseXml::generateFileXml(QByteArray &arr)
{
   QDomDocument domDoc;
   QDomProcessingInstruction instruction;
   instruction = domDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");
   domDoc.appendChild(instruction);

   QDomElement root = domDoc.createElement("Root");
   domDoc.appendChild(root);

   QDomElement countryList = domDoc.createElement("countryList");
   countryList.setAttribute("name",this->sName);
   countryList.setAttribute("continent",this->sContinent);

   foreach(Province* pro, this->m_plistProvince){
       QDomElement ele = domDoc.createElement("Province");
       ele.setAttribute("name",pro->sName);
       ele.setAttribute("referredName",pro->sReferredName);
       countryList.appendChild(ele);
       foreach(Zone*pZone, pro->m_plistZone){
           QDomElement eleChild = domDoc.createElement("Zone");
           eleChild.setAttribute("id",pZone->nId);
           eleChild.setAttribute("name",pZone->sName);
           eleChild.setAttribute("population",pZone->population);
           ele.appendChild(eleChild);
       }
   }

   arr = domDoc.toByteArray();
}

你可能感兴趣的:(qt,xml,开发语言)