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

news/2025/3/19 12:34:10/

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

#ifndef CFILEDATA_PRASEXML_H
#define CFILEDATA_PRASEXML_H
#include <QDomDocument>
#include <QDomElement>
#include <QList>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>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<Zone*>   m_plistZone;
};class CFileData_PraseXml
{
public:CFileData_PraseXml();~CFileData_PraseXml();
public:bool  ParseFileXml();bool  generateFileXml(QByteArray&arr);
private:QString sName;QString sContinent;QList<Province*>  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();
}

http://www.ppmy.cn/news/781953.html

相关文章

prophet路由详解 机会网络 DTN网络 ONE模拟器

ONE模拟器 简介代码代码详解 简介 prophet算法是机会网络中的经典算法&#xff0c;在Probabilistic routing in intermittently connected networks&#xff0c;2003中提出 论文链接 代码 /* * Copyright 2010 Aalto University, ComNet* Released under GPLv3. See LICENSE…

什么是DTD

DTD(Document Type Definition) 是一套关于标记符的语法规则。DTD 是一种保证XML文档格式正确的有效方法&#xff0c;可通过比较XML文档和DTD文件来看文档是否符合规范&#xff0c;元素和标签使用是否正确。 用于定义XML的结构和语法规则&#xff0c;避免出现数据的混乱 还用…

DTN学习,theONE模拟器网络相关资料整理

下面是一个百度空间的&#xff1a; http://hi.baidu.com/jensenliao 博客园的一篇博客&#xff1a;theONE模拟器简介&#xff08;主要讲述&#xff0c;软件配置&#xff0c;软件结构&#xff09; http://www.cnblogs.com/dreamfactory/archive/2012/07/27/2612215.html 博客…

tsn

TSN 1.如何提帧 1.1数据集准备 下载网址&#xff1a;http://crcv.ucf.edu/data/UCF101/UCF101.rar 下载成功后的UCF文件夹如下所示&#xff1a; 该文件夹下是各种动作的视频文件&#xff0c;共有101种类别 下图是UCF101在进行训练和测试时&#xff0c;分割的依据文件 1.2…

【C语言初阶(10)】函数练习题

文章目录 1. 判断素数2. 判断闰年3. 函数实现二分查找4. 记录函数调用次数 1. 判断素数 题目内容 写一个函数可以判断一个数是不是素数。 素数 素数也叫做质数&#xff0c;一个只能被 1 和它本身整除的数字称之为素数。 例如&#xff1a;7 这个数字只能被 1 和 它本身&#x…

DTD详解

基本概述 文档类型定义(Document Type Definition)是一套为了进行程序间的数据交换而建立的关于标记符的语法规则。它是标准通用标记语言(SGML)和可扩展标记语言(XML)1.0版规格的一部分&#xff0c;文档可根据某种DTD语法规则验证格式是否符合此规则。文档类型定义也可用做保证…

DHT网络简介

DHT网络全称为分布式哈希表网络(Distributed Hash Table net)&#xff0c;是一种由键值对唯一标识的信息按某种协议被分散在多个节点上的“非中心化服务”网络。可在分布式对等网络环境中进行存储、检索、查询、等管理数据。其数据大规模分散在多个节点上的网络。可以有效避免单…

DTN中基于两跳ACK 确认机制的备用副本转发算法

DTN中基于两跳ACK 确认机制的备用副本转发算法 摘要 针对DTN在遇到路由空洞问题&#xff08;由于某些原因&#xff0c;没有合适的下一跳节点&#xff09;时缺乏有效回避路由空洞的方法导致信息端到端传输时延较大的问题&#xff0c;提出了DTN中基于两跳ACK &#xff08;ACK1 …