工程中用的pugixml,读写做的比较复杂,要求存储的属性顺序固定、内存中的xml增删改可回滚、xml文件可以递归链接解析、支持xsd校验、支持xsd约束查询判断…
小工具中就简化了,没必要引入太多复杂代码,所以就用的以下简单版本。
xml结构如下:
<root>
<db_infor hostname="ll\SQL2008" dbname="db_name" user="sa" passwd="avefvfA$&%^*66666" />
<process_group>
<process id="0" type="NULL" desc="无处理"/>
<process id="1" type="TimeDomainAnalysis" desc="时域分析"/>
</process_group>
<data_group>
<data id="0" tablename="base" col_name="A" time_from="" time_to="" id_from="0" id_to="100" col_value_from="" col_value_to="" process_id="0" desc="A列0-100行原始数据" />
<data id="1" data_id="0" process_id="1" desc="A列0-100行数据时域分析后" />
</data_group>
<view_group>
<view id="0" type="table" desc="表格" />
<view id="1" type="waveform" desc="波形图" />
</view_group>
<data_view_group>
<data_view id="0" data="0" view_id="0" desc="A列0-100行原始数据的表格展示" />
<data_view id="1" data="0" view_id="1" desc="A列0-100行原始数据的波形图展示" />
<data_view id="2" data="1" view_id="0" desc="A列0-100行数据时域分析后的表格展示" />
<data_view id="3" data="1" view_id="1" desc="A列0-100行数据时域分析后的波形图展示" />
</data_view_group>
</root>
#pragma once
#include <QObject>
#include <QString>
#include <QtXml/QDomDocument>
#include "mem_data.h"
class xml_node{
public:
virtual ~xml_node(){}
public:
xml_node() :id_(0), desc_(""){}
xml_node(const int id) :id_(id), desc_(""){}
xml_node(const QString &desc) :id_(0), desc_(desc){}
xml_node(const int id, const QString &desc) :id_(id), desc_(desc){}
xml_node(const xml_node &t) :id_(t.id_), desc_(t.desc_){}
public:
inline xml_node &operator=(const xml_node &t){
if (this == &t)return *this;
id_ = t.id_;
desc_ = t.desc_;
return *this;
}
inline bool operator==(const xml_node &t)const{
return id_ == t.id_;
}
inline bool operator!=(const xml_node &t)const{
return id_ != t.id_;
}
inline bool operator<(const xml_node &t)const{
return id_ < t.id_;
}
inline bool operator<=(const xml_node &t)const{
return id_ <= t.id_;
}
inline bool operator>(const xml_node &t)const{
return id_ > t.id_;
}
inline bool operator>=(const xml_node &t)const{
return id_ >= t.id_;
}
public:
inline void set_id(const int id){
id_ = id;
}
inline int id()const{
return id_;
}
inline void set_desc(const QString &desc){
desc_ = desc;
}
inline QString desc()const{
return desc_;
}
// inline void set_tag_name(const QString &tag_name){
// tag_name_ = tag_name;
// }
// inline QString tag_name()const{
// return tag_name_;
// }
public:
void read_form_xml_dom(const QDomElement &dom);
void write_to_xml_dom(QDomElement &dom);
public:
int id_;//组内编号,默认为0
QString desc_;//节点描述
//QString tag_name_;//xml节点名称。简单化,暂不实现。
};
typedef std::vector<xml_node*> xml_node_vec;
typedef std::map<int, xml_node*> xml_node_map;
class db_infor : public xml_node{
public:
db_infor() :xml_node(QObject::tr("数据库信息")){}
virtual ~db_infor(){}
db_infor(const db_infor &t) :xml_node(t),
host_name_(t.host_name_), db_name_(t.db_name_),
username_(t.username_), password_(t.password_){}
inline db_infor &operator=(const db_infor &t){
if (this == &t)return *this;
xml_node::operator=(t);
host_name_ = t.host_name_;
db_name_ = t.db_name_;
username_ = t.username_;
password_ = t.password_;
return *this;
}
void read_form_xml_dom(const QDomElement &dom);
void write_to_xml_dom(QDomElement &dom);
public:
QString host_name_;
QString db_name_;
QString username_;
QString password_;
};
typedef std::vector<db_infor*> db_infor_vec;
typedef std::map<int, db_infor*> db_infor_map;
enum process_type{
kEPT_NULL = 0, //不处理
kEPT_TimeDomainAnalysis, //时域分析 Time domain analysis
kEPT_FrequencyDomainAnalysis, //频域分析 Frequency domain analysis
kEPT_FourierTransform, //傅里叶变换 Fourier transform
kEPT_WaveletAnalysis, //小波分析 Wavelet analysis
kEPT_Differential, //微分 differential
kEPT_FirstIntegral, //一次积分 First integral
kEPT_QuadraticIntegral, //二次积分 Quadratic integral
kEPT_AutocorrelationAnalysis, //自相关分析 Autocorrelation analysis
kEPT_CrossCorrelationAnalysis,//互相关分析 Cross correlation analysis
};
typedef std::map<std::string, process_type> process_type_str2enum_map;
typedef std::map<process_type, std::string> process_type_enum2str_map;
class process_node : public xml_node{
public:
process_node():type_("NULL"){}
virtual ~process_node(){}
process_node(const process_node &t) :xml_node(t), type_(t.type_){}
inline process_node &operator=(const process_node &t){
if (this == &t)return *this;
xml_node::operator=(t);
type_ = t.type_;
return *this;
}
void read_form_xml_dom(const QDomElement &dom);
void write_to_xml_dom(QDomElement &dom);
public:
QString type_;//输出处理的类型,默认值NULL表示不处理。可以改为枚举
};
typedef std::vector<process_node*> process_vec;
typedef std::map<int, process_node*> process_map;
class data_node:public xml_node{
public:
data_node() :data_id_(-1), process_id_(-1), p_mem_data_(NULL){}
virtual ~data_node(){
if (p_mem_data_ != NULL)
{
if (p_mem_data_->p_father() == this)
{
delete p_mem_data_;//只有父节点才可以删除
}
p_mem_data_ = NULL;
}
}
data_node(const data_node &t) :xml_node(t), p_mem_data_(NULL),
data_id_(t.data_id_), process_id_(t.process_id_),
table_name_(t.table_name_), col_name_(t.col_name_),
time_from_(t.time_from_), time_to_(t.time_to_),
id_from_(t.id_from_), id_to_(t.id_to_),
col_value_from_(t.col_value_from_), col_value_to_(t.col_value_to_){}
inline data_node &operator=(const data_node &t){
if (this == &t)return *this;
xml_node::operator=(t);
data_id_ = t.data_id_;
process_id_ = t.process_id_;
//
table_name_ = t.table_name_;
col_name_ = t.col_name_;
time_from_ = t.time_from_;
time_to_ = t.time_to_;
id_from_ = t.id_from_;
id_to_ = t.id_to_;
col_value_from_ = t.col_value_from_;
col_value_to_ = t.col_value_to_;
//
p_mem_data_ = t.p_mem_data_;
return *this;
}
void read_form_xml_dom(const QDomElement &dom);
void write_to_xml_dom(QDomElement &dom);
public:
int data_id_;//基础数据的id,-1无效,如果这个有效,其它属性基本无效,除了process_id_有效。
int process_id_;//获得数据集后进行的处理类型id,-1表示不做处理
public://以下只为获得处理前的数据集,与data_id_不共存。
QString table_name_;
QString col_name_;
QString time_from_;
QString time_to_;
int id_from_;
int id_to_;
QString col_value_from_;
QString col_value_to_;
private:
mem_data *p_mem_data_;
public:
mem_data *get_mem_data(){
if (p_mem_data_ == NULL){
p_mem_data_ = new mem_data(this);
}
return p_mem_data_;
}
};
typedef std::vector<data_node*> data_vec;
typedef std::map<int, data_node*> data_map;
enum view_type{
kEVT_table = 0, //表格
kEVT_waveform, //波形图
};
typedef std::map<std::string, view_type> view_type_str2enum_map;
typedef std::map<view_type, std::string> view_type_enum2str_map;
class view_node : public xml_node{
public:
view_node() :type_("table"){}
virtual ~view_node(){}
view_node(const view_node &t) :xml_node(t), type_(t.type_){}
inline view_node &operator=(const view_node &t){
if (this == &t)return *this;
xml_node::operator=(t);
type_ = t.type_;
return *this;
}
void read_form_xml_dom(const QDomElement &dom);
void write_to_xml_dom(QDomElement &dom);
public:
QString type_;//显示类型,默认值table表示表格类型。可以改为枚举.
//waveform波形图...
};
typedef std::vector<view_node*> view_vec;
typedef std::map<int, view_node*> view_map;
class data_view : public xml_node{
public:
data_view() :data_id_(-1),view_id_(-1){}
virtual ~data_view(){}
data_view(const data_view &t) :xml_node(t), data_id_(t.data_id_),view_id_(t.view_id_){}
inline data_view &operator=(const data_view &t){
if (this == &t)return *this;
xml_node::operator=(t);
data_id_ = t.data_id_;
view_id_ = t.view_id_;
return *this;
}
void read_form_xml_dom(const QDomElement &dom);
void write_to_xml_dom(QDomElement &dom);
public:
int data_id_;//显示用数据
int view_id_;//显示方式
};
typedef std::vector<data_view*> data_view_vec;
typedef std::map<int, data_view*> data_view_map;
class xml_data{
public:
xml_data();
~xml_data();
public://xml内存数据
db_infor db_infor_;//数据库连接信息
//
process_vec process_vec_;//数据处理方案集合
process_map process_map_;
//
data_vec data_vec_;//数据集合
data_map data_map_;
//
view_vec view_vec_;//视图类型集合
view_map view_map_;
//
data_view_vec data_view_vec_;//数据视图集合
data_view_map data_view_map_;
public://读写xml文件
void RemoveFileHeadFlag(QByteArray &xml);
bool OpenXml(const QString &file_name);
bool SaveXml(const QString &file_name);
public://读写xml节点
bool GetDataFromXml(const QDomElement &root_dom, db_infor &db_infor_t);
void WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, db_infor &db_infor_t);
//
bool GetDataFromXml(const QDomElement &root_dom, process_vec &data_vec_t, process_map &data_map_t);
void WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, process_vec &data_vec_t);
//
bool GetDataFromXml(const QDomElement &root_dom, data_vec &data_vec_t, data_map &data_map_t);
void WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, data_vec &data_vec_t);
//
bool GetDataFromXml(const QDomElement &root_dom, view_vec &data_vec_t, view_map &data_map_t);
void WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, view_vec &data_vec_t);
//
bool GetDataFromXml(const QDomElement &root_dom, data_view_vec &data_vec_t, data_view_map &data_map_t);
void WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, data_view_vec &data_vec_t);
public://字符串与枚举类型互相转换的一组函数
static process_type process_type_to_enum(const QString &process_type_str);
static QString enum_to_process_type_str(const process_type process_type_id);
static view_type view_type_to_enum(const QString &view_type_str);
static QString enum_to_view_type_str(const view_type view_type_id);
};
#include "xml_data.h"
#include <QFile>
#include <QtXml/QXmlInputSource>
#include "logger.h"
void xml_node::read_form_xml_dom(const QDomElement &dom){
if (dom.hasAttribute("id")){
id_ = dom.attribute("id").toInt();
}
else{
id_ = -1;
}
desc_ = dom.attribute("desc");
}
void xml_node::write_to_xml_dom(QDomElement &dom){
if (id_ >= 0){
dom.setAttribute("id", QString::number(id_));
}
if (desc_.length() > 0)
{
dom.setAttribute("desc", desc_);
}
}
void db_infor::read_form_xml_dom(const QDomElement &dom){
xml_node::read_form_xml_dom(dom);
host_name_ = dom.attribute("host_name");
db_name_ = dom.attribute("db_name");
username_ = dom.attribute("username");
password_ = dom.attribute("password");
}
void db_infor::write_to_xml_dom(QDomElement &dom){
xml_node::write_to_xml_dom(dom);
if (host_name_.length() > 0){
dom.setAttribute("host_name", host_name_);
}
if (db_name_.length() > 0){
dom.setAttribute("db_name", db_name_);
}
if (username_.length() > 0){
dom.setAttribute("username", username_);
}
if (password_.length() > 0){
dom.setAttribute("password", password_);
}
}
void process_node::read_form_xml_dom(const QDomElement &dom){
xml_node::read_form_xml_dom(dom);
type_ = dom.attribute("type");
}
void process_node::write_to_xml_dom(QDomElement &dom){
xml_node::write_to_xml_dom(dom);
if (type_.length() > 0){
dom.setAttribute("type", type_);
}
}
void data_node::read_form_xml_dom(const QDomElement &dom){
xml_node::read_form_xml_dom(dom);
if (dom.hasAttribute("data_id")){
data_id_ = dom.attribute("data_id").toInt();
}
else{
data_id_ = -1;
}
if (dom.hasAttribute("process_id")){
process_id_ = dom.attribute("process_id").toInt();
}
else{
process_id_ = -1;
}
if (dom.hasAttribute("id_from")){
id_from_ = dom.attribute("id_from").toInt();
}
else{
id_from_ = -1;
}
if (dom.hasAttribute("id_to")){
id_to_ = dom.attribute("id_to").toInt();
}
else{
id_to_ = -1;
}
table_name_ = dom.attribute("table_name");
col_name_ = dom.attribute("col_name");
time_from_ = dom.attribute("time_from");
time_to_ = dom.attribute("time_to");
col_value_from_ = dom.attribute("col_value_from");
col_value_to_ = dom.attribute("col_value_to");
}
void data_node::write_to_xml_dom(QDomElement &dom){
xml_node::write_to_xml_dom(dom);
if (data_id_ >= 0){
dom.setAttribute("data_id", QString::number(data_id_));
}
if (process_id_ >= 0){
dom.setAttribute("process_id", QString::number(process_id_));
}
if (id_from_ >= 0){
dom.setAttribute("id_from", QString::number(id_from_));
}
if (id_to_ >= 0){
dom.setAttribute("id_to", QString::number(id_to_));
}
if (table_name_.length() > 0){
dom.setAttribute("table_name", table_name_);
}
if (col_name_.length() > 0){
dom.setAttribute("col_name", col_name_);
}
if (time_from_.length() > 0){
dom.setAttribute("time_from", time_from_);
}
if (time_to_.length() > 0){
dom.setAttribute("time_to", time_to_);
}
if (col_value_from_.length() > 0){
dom.setAttribute("col_value_from", col_value_from_);
}
if (col_value_to_.length() > 0){
dom.setAttribute("col_value_to", col_value_to_);
}
}
void view_node::read_form_xml_dom(const QDomElement &dom){
xml_node::read_form_xml_dom(dom);
type_ = dom.attribute("type");
}
void view_node::write_to_xml_dom(QDomElement &dom){
xml_node::write_to_xml_dom(dom);
if (type_.length() > 0){
dom.setAttribute("type", type_);
}
}
void data_view::read_form_xml_dom(const QDomElement &dom){
xml_node::read_form_xml_dom(dom);
if (dom.hasAttribute("data_id")){
data_id_ = dom.attribute("data_id").toInt();
}
else{
data_id_ = -1;
}
if (dom.hasAttribute("view_id")){
view_id_ = dom.attribute("view_id").toInt();
}
else{
view_id_ = -1;
}
}
void data_view::write_to_xml_dom(QDomElement &dom){
xml_node::write_to_xml_dom(dom);
if (data_id_ >= 0){
dom.setAttribute("data_id", QString::number(data_id_));
}
if (view_id_ >= 0){
dom.setAttribute("view_id", QString::number(view_id_));
}
}
xml_data::xml_data()
{
}
xml_data::~xml_data()
{
for (auto v : process_vec_)
{
delete v;
}
process_vec_.clear();
//
for (auto v : data_vec_)
{
delete v;
}
data_vec_.clear();
//
for (auto v : view_vec_)
{
delete v;
}
view_vec_.clear();
//
for (auto v : data_view_vec_)
{
delete v;
}
data_view_vec_.clear();
}
process_type xml_data::process_type_to_enum(const QString &process_type_str)
{
static bool b = false;
static process_type_str2enum_map str_to_type_map;
if (!b)
{
str_to_type_map.insert(process_type_str2enum_map::value_type("NULL", kEPT_NULL));
str_to_type_map.insert(process_type_str2enum_map::value_type("TimeDomainAnalysis", kEPT_TimeDomainAnalysis));
str_to_type_map.insert(process_type_str2enum_map::value_type("FrequencyDomainAnalysis", kEPT_FrequencyDomainAnalysis));
str_to_type_map.insert(process_type_str2enum_map::value_type("FourierTransform", kEPT_FourierTransform));
str_to_type_map.insert(process_type_str2enum_map::value_type("WaveletAnalysis", kEPT_WaveletAnalysis));
str_to_type_map.insert(process_type_str2enum_map::value_type("Differential", kEPT_Differential));
str_to_type_map.insert(process_type_str2enum_map::value_type("FirstIntegral", kEPT_FirstIntegral));
str_to_type_map.insert(process_type_str2enum_map::value_type("QuadraticIntegral", kEPT_QuadraticIntegral));
str_to_type_map.insert(process_type_str2enum_map::value_type("AutocorrelationAnalysis", kEPT_AutocorrelationAnalysis));
str_to_type_map.insert(process_type_str2enum_map::value_type("CrossCorrelationAnalysis", kEPT_CrossCorrelationAnalysis));
//
b = true;
}
process_type_str2enum_map::iterator i = str_to_type_map.find(process_type_str.toStdString());
if (i == str_to_type_map.end())
{
return kEPT_NULL;
}
return i->second;
}
QString xml_data::enum_to_process_type_str(const process_type process_type_id)
{
static bool b = false;
static process_type_enum2str_map type_to_str_map;
if (!b)
{
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_NULL, "NULL"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_TimeDomainAnalysis, "TimeDomainAnalysis"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_FrequencyDomainAnalysis, "FrequencyDomainAnalysis"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_FourierTransform, "FourierTransform"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_WaveletAnalysis, "WaveletAnalysis"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_Differential, "Differential"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_FirstIntegral, "FirstIntegral"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_QuadraticIntegral, "QuadraticIntegral"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_AutocorrelationAnalysis, "AutocorrelationAnalysis"));
type_to_str_map.insert(process_type_enum2str_map::value_type(kEPT_CrossCorrelationAnalysis, "CrossCorrelationAnalysis"));
//
b = true;
}
process_type_enum2str_map::iterator i = type_to_str_map.find(process_type_id);
if (i == type_to_str_map.end())
{
return "NULL";
}
return QString::fromStdString(i->second);
}
view_type xml_data::view_type_to_enum(const QString &view_type_str)
{
static bool b = false;
static view_type_str2enum_map str_to_type_map;
if (!b)
{
str_to_type_map.insert(view_type_str2enum_map::value_type("table", kEVT_table));
str_to_type_map.insert(view_type_str2enum_map::value_type("waveform", kEVT_waveform));
//
b = true;
}
view_type_str2enum_map::iterator i = str_to_type_map.find(view_type_str.toStdString());
if (i == str_to_type_map.end())
{
return kEVT_table;
}
return i->second;
}
QString xml_data::enum_to_view_type_str(const view_type view_type_id)
{
static bool b = false;
static view_type_enum2str_map type_to_str_map;
if (!b)
{
type_to_str_map.insert(view_type_enum2str_map::value_type(kEVT_table, "table"));
type_to_str_map.insert(view_type_enum2str_map::value_type(kEVT_waveform, "waveform"));
//
b = true;
}
view_type_enum2str_map::iterator i = type_to_str_map.find(view_type_id);
if (i == type_to_str_map.end())
{
return "table";
}
return QString::fromStdString(i->second);
}
void xml_data::RemoveFileHeadFlag(QByteArray &xml){
if (xml[0] == char(0xef) && xml[1] == char(0xbb) && xml[2] == char(0xbf)){
xml.remove(0, 3);
}
}
bool xml_data::OpenXml(const QString &file_name)
{
QFile file(file_name);
if (!file.open(QFile::ReadOnly)){
return false;
}
QByteArray xml = file.readAll();
file.close();
RemoveFileHeadFlag(xml);
QXmlInputSource xmlInputSource;
QDomDocument domDocument;
domDocument.clear();
QString errorStr;
int errorLine;
int errorColumn;
//
xmlInputSource.setData(xml);
if (!domDocument.setContent(&xmlInputSource, true, &errorStr, &errorLine, &errorColumn)) {
throw QString(QObject::tr("xml内容异常: line(%1) column(%2) %3").arg(errorLine).arg(errorColumn).arg(errorStr));
}
//
QDomElement xml_root = domDocument.documentElement();
if (xml_root.tagName() == "root"){
bool b = GetDataFromXml(xml_root, db_infor_);
b = GetDataFromXml(xml_root, process_vec_, process_map_) && b;
b = GetDataFromXml(xml_root, data_vec_, data_map_) && b;
b = GetDataFromXml(xml_root, view_vec_, view_map_) && b;
b = GetDataFromXml(xml_root, data_view_vec_, data_view_map_) && b;
LogX(QObject::tr("open config xml file:%1").arg(file_name).toStdString());
return b;
}
return false;
}
bool xml_data::SaveXml(const QString &file_name)
{
QDomDocument domDocument;
QDomElement root_node = domDocument.createElement("root");
domDocument.appendChild(root_node);
//
WriteDataToXml(domDocument, root_node, db_infor_);
WriteDataToXml(domDocument, root_node, process_vec_);
WriteDataToXml(domDocument, root_node, data_vec_);
WriteDataToXml(domDocument, root_node, view_vec_);
WriteDataToXml(domDocument, root_node, data_view_vec_);
//保存到xml文件
QFile file;
file.setFileName(file_name);
if (!file.open(QFile::WriteOnly | QFile::Truncate | QFile::Text)){//Text方式下有回车。
return false;
}
QTextStream ts(&file);
ts.setCodec("UTF-8");//UTF必须大写才会被正确识别。
domDocument.save(ts, 4, QDomNode::EncodingFromTextStream);
file.close();
LogX(QObject::tr("save config xml file:%1").arg(file_name).toStdString());
return true;
}
bool xml_data::GetDataFromXml(const QDomElement &root_dom, db_infor &db_infor_t)
{
QDomNodeList db_infor_list = root_dom.elementsByTagName("db_infor");
if (db_infor_list.length() == 1)
{
QDomElement db_infor_dom = db_infor_list.item(0).toElement();
db_infor_t.read_form_xml_dom(db_infor_dom);//未检查数据库参数错误
return true;
}
else
{
return false;
}
}
void xml_data::WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, db_infor &db_infor_t)
{
QDomElement db_infor_dom = domDocument.createElement("db_infor");
db_infor_t.write_to_xml_dom(db_infor_dom);
root_dom.appendChild(db_infor_dom);
}
bool xml_data::GetDataFromXml(const QDomElement &root_dom, process_vec &data_vec_t, process_map &data_map_t)
{
QDomNodeList process_group_list = root_dom.elementsByTagName("process_group");
if (process_group_list.length() == 1)
{
QDomElement process_group_dom = process_group_list.item(0).toElement();
QDomNodeList process_list = process_group_dom.elementsByTagName("process");
int iCount = process_list.length();
for (int i = 0; i < iCount; ++i)
{
QDomElement process_dom = process_list.item(i).toElement();
process_node *p_process = new process_node;
p_process->read_form_xml_dom(process_dom);
if (data_map_t.find(p_process->id_) == data_map_t.end())
{
data_map_t.insert(process_map::value_type(p_process->id_, p_process));
data_vec_t.push_back(p_process);
}
else
{
delete p_process;
return false;//id重复
}
}
return true;
}
else
{
return false;
}
return false;
}
void xml_data::WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, process_vec &data_vec_t)
{
QDomElement data_group_dom = domDocument.createElement("process_group");
for each (process_node *p_data in data_vec_t)
{
QDomElement data_dom = domDocument.createElement("process");
p_data->write_to_xml_dom(data_dom);
data_group_dom.appendChild(data_dom);
}
root_dom.appendChild(data_group_dom);
}
bool xml_data::GetDataFromXml(const QDomElement &root_dom, data_vec &data_vec_t, data_map &data_map_t)
{
QDomNodeList data_group_list = root_dom.elementsByTagName("data_group");
if (data_group_list.length() == 1)
{
QDomElement data_group_dom = data_group_list.item(0).toElement();
QDomNodeList data_list = data_group_dom.elementsByTagName("data");
int iCount = data_list.length();
for (int i = 0; i < iCount; ++i)
{
QDomElement data_dom = data_list.item(i).toElement();
data_node *p_data = new data_node;
p_data->read_form_xml_dom(data_dom);
if (data_map_t.find(p_data->id_) == data_map_t.end())
{
data_map_t.insert(data_map::value_type(p_data->id_, p_data));
data_vec_t.push_back(p_data);
}
else
{
delete p_data;
return false;//id重复
}
}
return true;
}
else
{
return false;
}
return false;
}
void xml_data::WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, data_vec &data_vec_t)
{
QDomElement data_group_dom = domDocument.createElement("data_group");
for each (data_node *p_data in data_vec_t)
{
QDomElement data_dom = domDocument.createElement("data");
p_data->write_to_xml_dom(data_dom);
data_group_dom.appendChild(data_dom);
}
root_dom.appendChild(data_group_dom);
}
bool xml_data::GetDataFromXml(const QDomElement &root_dom, view_vec &data_vec_t, view_map &data_map_t)
{
QDomNodeList data_group_list = root_dom.elementsByTagName("view_group");
if (data_group_list.length() == 1)
{
QDomElement data_group_dom = data_group_list.item(0).toElement();
QDomNodeList data_list = data_group_dom.elementsByTagName("view");
int iCount = data_list.length();
for (int i = 0; i < iCount; ++i)
{
QDomElement data_dom = data_list.item(i).toElement();
view_node *p_data = new view_node;
p_data->read_form_xml_dom(data_dom);
if (data_map_t.find(p_data->id_) == data_map_t.end())
{
data_map_t.insert(view_map::value_type(p_data->id_, p_data));
data_vec_t.push_back(p_data);
}
else
{
delete p_data;
return false;//id重复
}
}
return true;
}
else
{
return false;
}
return false;
}
void xml_data::WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, view_vec &data_vec_t)
{
QDomElement data_group_dom = domDocument.createElement("view_group");
for each (view_node *p_data in data_vec_t)
{
QDomElement data_dom = domDocument.createElement("view");
p_data->write_to_xml_dom(data_dom);
data_group_dom.appendChild(data_dom);
}
root_dom.appendChild(data_group_dom);
}
bool xml_data::GetDataFromXml(const QDomElement &root_dom, data_view_vec &data_vec_t, data_view_map &data_map_t)
{
QDomNodeList data_group_list = root_dom.elementsByTagName("data_view_group");
if (data_group_list.length() == 1)
{
QDomElement data_group_dom = data_group_list.item(0).toElement();
QDomNodeList data_list = data_group_dom.elementsByTagName("data_view");
int iCount = data_list.length();
for (int i = 0; i < iCount; ++i)
{
QDomElement data_dom = data_list.item(i).toElement();
data_view *p_data = new data_view;
p_data->read_form_xml_dom(data_dom);
if (data_map_t.find(p_data->id_) == data_map_t.end())
{
data_map_t.insert(data_view_map::value_type(p_data->id_, p_data));
data_vec_t.push_back(p_data);
}
else
{
delete p_data;
return false;//id重复
}
}
return true;
}
else
{
return false;
}
return false;
}
void xml_data::WriteDataToXml(QDomDocument &domDocument, QDomElement &root_dom, data_view_vec &data_vec_t)
{
QDomElement data_group_dom = domDocument.createElement("data_view_group");
for each (data_view *p_data in data_vec_t)
{
QDomElement data_dom = domDocument.createElement("data_view");
p_data->write_to_xml_dom(data_dom);
data_group_dom.appendChild(data_dom);
}
root_dom.appendChild(data_group_dom);
}