xml 文件
<?xml version='1.0' encoding='GB2312'?>
<rtdbs>
<rtdb name="system" sync="0">
<data name="HmiLoopCount" length="4" type="ulong" description="屏幕刷新次数计数值" init="0"/>
<data name="HideMainWindow" length="1" type="bit" description="=1时隐藏主窗口" init="1"/>
<data name="CurDateTime" length="4" type="ulong" description="表示1970/1/1以来的秒数" init="0"/>
<data name="HmiHeartbeat" length="1" type="bit" description="人机界面运行心跳" init="0"/>
<data name="HmidbDefCycleTime" length="4" type="ulong" description="人机界面默认刷新周期(毫秒)" init="500"/>
<data name="Year" length="2" type="ushort" description="当前日期(年)" init="0"/>
<data name="Month" length="1" type="uchar" description="当前日期(月)" init="0"/>
<data name="Day" length="1" type="uchar" description="当前日期(日)" init="0"/>
<data name="Hour" length="1" type="uchar" description="当前日期(小时)" init="0"/>
<data name="Minute" length="1" type="uchar" description="当前日期(分)" init="0"/>
<data name="Second" length="1" type="uchar" description="当前日期(秒)" init="0"/>
<data name="dcs_rtdb_server_ip" length="20" type="string" description="实时数据库服务器IP地址" init="127.0.0.1"/>
<data name="dcs_rtdb_server_timeout" length="4" type="long" description="" init="500"/>
<data name="simu_server_ip" length="20" type="string" description="实时数据库服务器IP地址" init="127.0.0.1"/>
<data name="simu_server_timeout" length="4" type="long" description="" init="500"/>
</rtdb>
</rtdbs>
rtdbs为根节点,< rtdb name=“system” sync=“0”> rtdb表示一个数据库,name表示数据库名字,每个data表示一条数据
项目配置
pro文件下加上
QT += xml
#include <QFile>
#include <QDebug>
#include <QDomNodeList>
#include <QDomNode>
#include <QStringList>
QString filepath = "D:/qt_project/QDomNode_demo/doc/rtdb.xml"; // xml文件所在路径,最好先用绝对路径
1 指定数据库名,遍历下面的所有数据(查全部)
传入的是system 所以遍历了数据库名为system 下的数据
bool MainWindow::display_xml(QString sname)
{ // 显示xml
//打开文件
QFile file(filepath);
if(!file.open(QFile::ReadOnly))
return false;
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
QDomElement root=doc.documentElement(); // 找到根标签
QDomNodeList rtdb_lists=root.elementsByTagName("rtdb"); // 根标签下的所有rtdb标签
for(int i=0;i<rtdb_lists.count();i++) // 遍历QDomNodeList
{
QDomElement rtnode=rtdb_lists.at(i).toElement(); // 将节点转换成QDomElement(如果节点不是元素返回空)
if(rtnode.attribute("name") == sname) // rtnode.attribute("name")(获取当前元素 name属性的值)
{
QDomNodeList nodelist=rtdb_lists.at(i).childNodes(); // 获取当前元素下的所有子节点
for(int j=0;j<nodelist.count();j++) // 遍历当前元素下的所有子节点
{
QDomElement node=nodelist.at(j).toElement(); // 将节点转换成QDomElement(如果节点不是元素返回空)
QStringList List; // 把data里面所有属性存储起来
List<<node.attribute("name") <<node.attribute("type") <<node.attribute("length") <<node.attribute("init") <<node.attribute("description");
qDebug()<<List;
}
}
}
}
2 添加数据库名(增加)
传入值为system111 ,新建了数据库
bool MainWindow::insert_db_xml(QString dbname)
{
//打开文件
QFile file(filepath);
if(!file.open(QFile::ReadOnly))
return false;
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
QDomElement root=doc.documentElement(); // 找到根节点
QDomElement rtdb = doc.createElement("rtdb"); // 创建一个rtdb元素
QDomElement title=doc.createElement("title");
rtdb.setAttribute("name",dbname); // 在rtdb下创建两个属性并且设置值
rtdb.setAttribute("sync","0");
// 小拓展,此项目不需要加
// QDomText text;
// text=doc.createTextNode("hahha"); //
// title.appendChild(text); // 把text放到title中间 <title>haha</title>
// rtdb.appendChild(title); // 把title 放到rtdb下 <rtdb><title>haha</title></rtdb>
root.appendChild(rtdb); // 把rtdb 添加到根节点中
if(!file.open(QFile::WriteOnly|QFile::Truncate))
return false;
else
{
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,4); //缩进4格
file.close();
}
return true;
}
3 指定数据库名下添加数据(指定分支增加)
参数构建
bool MainWindow::insert_db_data_xml(QString dbname,QStringList data)
{
//打开文件
QFile file(filepath);
if(!file.open(QFile::ReadOnly))
return false;
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
QDomElement root=doc.documentElement();
QDomNodeList lists=root.elementsByTagName("rtdb"); // 找到所有的rtdb
for(int i=0;i<lists.count();i++) // 一个个遍历
{
QDomElement rtnode=lists.at(i).toElement();
if(rtnode.attribute("name") == dbname) // 判断数据库是否一致
{
QDomElement addEl = doc.createElement("data"); // 创建data 标签
addEl.setAttribute("name", data[0]); // 把传递过来的data数据分别写入
addEl.setAttribute("type", data[1]);
addEl.setAttribute("length", data[2]);
addEl.setAttribute("init", data[3]);
addEl.setAttribute("description", data[4]);
rtnode.appendChild(addEl); // 把构建好的data 放入rtdb标签中
break; // 跳出
}
}
if(!file.open(QFile::WriteOnly|QFile::Truncate))
return false;
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,4); //缩进4格
file.close();
return true;
}
4 删除指定数据库(指定分支删除)
把刚才创建的system111删除了
bool MainWindow::del_db_xml(QString dbname)
{
//打开文件
QFile file(filepath);
if(!file.open(QFile::ReadOnly))
return false;
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
QDomElement root=doc.documentElement(); // 找到根标签
QDomNodeList lists=root.elementsByTagName("rtdb"); // 获取所有的数据库
for(int i=0;i<lists.count();i++) // 遍历所有数据库
{
QDomElement node=lists.at(i).toElement();
if(node.attribute("name") == dbname) // 找到需要删除的数据库名
{
root.removeChild(lists.at(i)); // 删除当前找到的
file.close();
break;
}
}
if(!file.open(QFile::WriteOnly|QFile::Truncate))
return false;
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,4); //缩进4格
file.close();
return true;
}
5 删除指定数据库下的指定数据(删除指定分支下的指定数据)
bool MainWindow::del_db_data_xml(QString dbname, QString data_name)
{
//打开文件
QFile file(filepath);
if(!file.open(QFile::ReadOnly))
return false;
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
QDomElement root=doc.documentElement(); // 找到根标签
QDomNodeList lists=root.elementsByTagName("rtdb");
for(int i=0;i<lists.count();i++) // 遍历所有数据库名字
{
QDomElement rtnode=lists.at(i).toElement();
if(rtnode.attribute("name") == dbname) // 找到指定数据库
{
QDomNodeList nodelist=lists.at(i).childNodes(); // 获取当前数据库的所有内容
for(int j=0;j<nodelist.count();j++) // 遍历数据库下的内容
{
QDomElement node=nodelist.at(j).toElement();
if(node.attribute("name") == data_name) // 找到data数据的名字
{
qDebug()<<"找到要删除的数据";
rtnode.removeChild(nodelist.at(j)); // 删除
break;
}
}
break;
}
}
if(!file.open(QFile::WriteOnly|QFile::Truncate))
return false;
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,4); //缩进4格
file.close();
return true;
}
6 修改指定数据库名(修改)
bool MainWindow::updata_db_name_xml(QString dbname,QString newname)
{
//打开文件
QFile file(filepath);
if(!file.open(QFile::ReadOnly))
return false;
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
QDomElement root=doc.documentElement(); // 找到根标签
QDomNodeList lists=root.elementsByTagName("rtdb");
for(int i=0;i<lists.count();i++) // 遍历所有数据库
{
QDomElement node=lists.at(i).toElement();
if(node.attribute("name") == dbname) // 找到需要修改的数据库
{
node.setAttribute("name",newname); // 进行重新设置就可以了
file.close();
break;
}
}
if(!file.open(QFile::WriteOnly|QFile::Truncate))
{
return false;
}
//输出到文件
QTextStream out_stream(&file);
doc.save(out_stream,4);
file.close();
return true;
}