博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt 读写XML文件
阅读量:5126 次
发布时间:2019-06-13

本文共 10571 字,大约阅读时间需要 35 分钟。

#ifndef SIM_XMLSTREAMREADER_H_#define SIM_XMLSTREAMREADER_H_#include 
#include
class XmlStreamReader{public: XmlStreamReader(); ~XmlStreamReader(); //读取文件 bool ReadFile(const QString &fileName); //跳过不能识别的元素 void SkipUnknownElement(); //读取XML文件的主体部分 void ReadWantDecideEditElement(); //解析想定基本信息 void ReadScenarioElement(); //解析想定文件实体信息 void ReadEntitiesElement(); //解析想定文件模型发布订阅关系 void ReadModelsElement(); //解析该实体的所有参数 void ReadParametersElement(); //解析发布关系 void ReadPublishElement(); //解析订阅关系 void ReadSubscribeElement();private: QXmlStreamReader m_qxml_reader;};#endif //SIM_XMLSTREAMREADER_H_
#include "xml_stream_reader.h"#include 
#include
#include
using namespace std;XmlStreamReader::XmlStreamReader(){}XmlStreamReader::~XmlStreamReader(){}bool XmlStreamReader::ReadFile(const QString &fileName){ QFile file(fileName); if (!file.open(QFile::ReadOnly | QFile::Text)) { std::cerr << "Error: Cannot read file " << qPrintable(fileName)<< ": " << qPrintable(file.errorString())<< std::endl; return false; } //如果文件打开成功,将它设置为QXmlStreamReader的输入设备 m_qxml_reader.setDevice(&file); //readNext()函数从输入流中读取下一个记号,如果成功而且还没有到达XML文件的结尾,函数将进入while循环, //每次调用readNext()函数,下一个记号就会被读取并变成当前的记号,readNext()函数报告error,atEnd返回true m_qxml_reader.readNext(); //当读取到XML文档结尾,或者发生错误,atEnd()函数返回true while (!m_qxml_reader.atEnd()) { //如果当前记号为StartElement,返回true if (m_qxml_reader.isStartElement()) { if (m_qxml_reader.name() == "WantDecideEdit") { ReadWantDecideEditElement(); } else { m_qxml_reader.raiseError(QObject::tr("Not a WantDecideEdit file")); } } else { m_qxml_reader.readNext(); } } file.close(); if (m_qxml_reader.hasError()) { std::cerr << "Error: Failed to parse file "<< qPrintable(fileName) << ": "<< qPrintable(m_qxml_reader.errorString()) << std::endl; return false; } else if (file.error() != QFile::NoError) { std::cerr << "Error: Cannot read file " << qPrintable(fileName)<< ": " << qPrintable(file.errorString())<< std::endl; return false; } return true;}void XmlStreamReader::ReadWantDecideEditElement(){ //跳过当前记号,只可能是WantDecideEdit记号 m_qxml_reader.readNext(); while (!m_qxml_reader.atEnd()) { //如果读取到关闭标签,只能是记号,跳过这个标签并跳出循环 if (m_qxml_reader.isEndElement()) { if (m_qxml_reader.name() == "WantDecideEdit") { m_qxml_reader.readNext(); break; } else m_qxml_reader.readNext(); } if (m_qxml_reader.isStartElement()) { if (m_qxml_reader.name() == "ScenarioInfo") { ReadScenarioElement(); } else if (m_qxml_reader.name() == "Entities") { ReadEntitiesElement(); } else if (m_qxml_reader.name() == "Models") { ReadModelsElement(); } else { SkipUnknownElement(); } } else { m_qxml_reader.readNext(); } }}void XmlStreamReader::ReadScenarioElement(){ m_qxml_reader.readNext(); while (!m_qxml_reader.atEnd()) { if (m_qxml_reader.isEndElement()) { if (m_qxml_reader.name() == "ScenarioInfo") { m_qxml_reader.readNext(); break; } else m_qxml_reader.readNext(); } if (m_qxml_reader.isStartElement()) { if (m_qxml_reader.name() == "Name") { QString name = m_qxml_reader.readElementText(); qDebug() << "name:" << name << endl; } else if (m_qxml_reader.name() == "ModificationDate") { QString modification_date = m_qxml_reader.readElementText(); qDebug() << "modification_date:" << modification_date << endl; } else if (m_qxml_reader.name() == "Description") { QString description = m_qxml_reader.readElementText(); qDebug() << "description:" << description << endl; return; } else { SkipUnknownElement(); } } else { m_qxml_reader.readNext(); } }}void XmlStreamReader::ReadParametersElement(){ m_qxml_reader.readNext(); while (!m_qxml_reader.atEnd()) { if (m_qxml_reader.isEndElement()) { if (m_qxml_reader.name() == "Parameters") { m_qxml_reader.readNext(); break; } else m_qxml_reader.readNext(); } if (m_qxml_reader.isStartElement()) { if (m_qxml_reader.name() == "Parameter") { QString name = m_qxml_reader.attributes().value("name").toString(); QString type = m_qxml_reader.attributes().value("type").toString(); QString display_name = m_qxml_reader.attributes().value("displayName").toString(); QString usage = m_qxml_reader.attributes().value("usage").toString(); QString value = m_qxml_reader.attributes().value("value").toString(); qDebug() << "name:" << name << " " << "type:" << type << " " << "display_name:" << display_name << " " << "usage:" << usage << " " << "value:" << value << endl; m_qxml_reader.readNext(); } else { SkipUnknownElement(); } } else { m_qxml_reader.readNext(); } }}void XmlStreamReader::ReadPublishElement(){ m_qxml_reader.readNext(); while (!m_qxml_reader.atEnd()) { if (m_qxml_reader.isEndElement()) { if (m_qxml_reader.name() == "Publish") { m_qxml_reader.readNext(); break; } else m_qxml_reader.readNext(); } if (m_qxml_reader.isStartElement()) { if (m_qxml_reader.name() == "Topic") { QString topic_name = m_qxml_reader.attributes().value("topicName").toString(); QString topic_param_name = m_qxml_reader.attributes().value("topicParamName").toString(); QString model_param_name = m_qxml_reader.attributes().value("modelParamName").toString(); qDebug() << "topicName:" << topic_name << ",topicParamName:" << topic_param_name << ",modelParamName:" << model_param_name << endl; m_qxml_reader.readNext(); } else { SkipUnknownElement(); } } else { m_qxml_reader.readNext(); } }}void XmlStreamReader::ReadSubscribeElement(){ m_qxml_reader.readNext(); while (!m_qxml_reader.atEnd()) { if (m_qxml_reader.isEndElement()) { if (m_qxml_reader.name() == "Subscribe") { m_qxml_reader.readNext(); break; } else m_qxml_reader.readNext(); } if (m_qxml_reader.isStartElement()) { if (m_qxml_reader.name() == "Topic") { QString topic_name = m_qxml_reader.attributes().value("topicName").toString(); QString topic_param_name = m_qxml_reader.attributes().value("topicParamName").toString(); QString model_param_name = m_qxml_reader.attributes().value("modelParamName").toString(); qDebug() << "topicName:" << topic_name << ",topicParamName:" << topic_param_name << ",modelParamName:" << model_param_name << endl; m_qxml_reader.readNext(); } else { SkipUnknownElement(); } } else { m_qxml_reader.readNext(); } }}void XmlStreamReader::ReadEntitiesElement(){ m_qxml_reader.readNext(); while (!m_qxml_reader.atEnd()) { if (m_qxml_reader.isEndElement()) { if (m_qxml_reader.name() == "Entities") { m_qxml_reader.readNext(); break; } else m_qxml_reader.readNext(); } if (m_qxml_reader.isStartElement()) { if (m_qxml_reader.name() == "Entity") { QString object_handle = m_qxml_reader.attributes().value("objectHandle").toString(); qDebug() << "objectHandle:" << object_handle << endl; QString model_uuid = m_qxml_reader.attributes().value("modelUUID").toString(); qDebug() << "modelUUID:" << model_uuid<
void writeIndexEntry(QXmlStreamWriter *xmlWriter, QTreeWidgetItem *item){    xmlWriter->writeStartElement("entry");    xmlWriter->writeAttribute("term", item->text(0));    QString pageString = item->text(1);    if (!pageString.isEmpty())    {        QStringList pages = pageString.split(", ");        foreach(QString page, pages)            xmlWriter->writeTextElement("page", page);    }    for (int i = 0; i < item->childCount(); ++i)        writeIndexEntry(xmlWriter, item->child(i));    xmlWriter->writeEndElement();}bool WriteXml(const QString &fileName, QTreeWidget *treeWidget){    QFile file(fileName);    if (!file.open(QFile::WriteOnly | QFile::Text))     {        std::cerr << "Error: Cannot write file "<< qPrintable(fileName) << ": "<< qPrintable(file.errorString()) << std::endl;        return false;    }    QXmlStreamWriter xmlWriter(&file);    //开启自动格式编辑功能,XML文档将以更友好易读的格式输出,在每一行都有明确显示数据递归结构的缩进    xmlWriter.setAutoFormatting(true);    //在XML文档首行写"
" xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("WantDecideEdit"); for (int i = 0; i < treeWidget->topLevelItemCount(); ++i) writeIndexEntry(&xmlWriter, treeWidget->topLevelItem(i)); xmlWriter.writeEndDocument(); file.close(); if (file.error()) { std::cerr << "Error: Cannot write file "<< qPrintable(fileName) << ": "<< qPrintable(file.errorString()) << std::endl; return false; } return true;}

 

转载于:https://www.cnblogs.com/LuckCoder/p/10942373.html

你可能感兴趣的文章
条件表达式
查看>>
整数比较操作符和逻辑操作符
查看>>
fragment间的数据传递
查看>>
下载评书
查看>>
学数学——林群讲的东西
查看>>
(爬虫)随机生成一个header
查看>>
一闪一闪亮晶晶
查看>>
网页状态码
查看>>
绘制UML图的工具
查看>>
物理引擎 书籍搜集
查看>>
excel绘制多列 其中一列作为横坐标 ; 数值拟合
查看>>
git add命令行添加文件、文件夹以及撤销文件add的方法
查看>>
windows下的文件管理工具--total commander
查看>>
sizeof(类名字)
查看>>
四元数
查看>>
功率谱密度如何理解
查看>>
git clean解决 GIT error: The following untracked working tree files would be overwritten
查看>>
windows下的计算时间间隔 -- GetTickCount()
查看>>
Excel在数据表中悬停鼠标显示数据值
查看>>
UML类图知识
查看>>