【XML文件解析(二)】
文章目录
概要:本期主要讲解Qt中的QDomDocument类来实现XML文件的解析。
XML操作类如下图:
XML文件如下图:
一、创建xml文件
函数名:CreateXmlFile
参数:_path(传入xml文件生成路径)
功能:在指定位置生成一个空的xml文件
bool XmlOperation::CreateXmlFile(QString _path)
{
if(_path == "")
{
qDebug()<<"输入xml文件路径有误!";
return false;
}
QFile _file(_path);
if(!_file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
qDebug()<<"Open "<<_path<<"failed!";
return false;
}
QDomDocument _doc;
//xml文件声明
QDomProcessingInstruction _instruction = _doc.createProcessingInstruction("xml","version=\'1.0\' encoding=\'utf-8\'");
_doc.appendChild(_instruction);
//创建根元素
QDomElement _root = _doc.createElement("root");
_doc.appendChild(_root);
//写入到文件
QTextStream outFile(&_file);
//缩进4格
_doc.save(outFile, 4);
_file.close();
return true;
}
二、在XML文件中插入元素
函数名:AddXmlNode
参数:_path(传入xml文件生成路径)_map(传入元素名和对应值)
功能:在指定位置的xml文件中插入元素
bool XmlOperation::AddXmlNode(QString _path, QMap<QString, QString> _map)
{
if(_path == "")
{
qDebug()<<"输入xml文件路径有误!";
return false;
}
QFile _file(_path);
if(!_file.open(QIODevice::ReadOnly))
{
qDebug()<<"Open "<<_path<<"failed!";
return false;
}
//移动光标到第0行
_file.seek(0);
//错误检测
QDomDocument _doc;
QString _error;
int _row;
int _column;
if(!_doc.setContent(&_file, &_error, &_row, &_column))
{
qDebug()<<_error<<_row<<_column;
return false;
}
_file.close();
//获取根元素
QDomElement _root = _doc.documentElement();
//创建子元素
QMapIterator<QString,QString> _itor(_map);
while(_itor.hasNext())
{
_itor.next();
//子元素
QDomElement _item = _doc.createElement(_itor.key());
//元素内容
QDomText _text = _doc.createTextNode(_itor.value());
_item.appendChild(_text);
_root.appendChild(_item);
}
if(!_file.open(QFile::WriteOnly|QFile::Truncate))
{
qDebug()<<"add content faild!";
return false;
}
QTextStream outStream(&_file);
_doc.save(outStream,4);
_file.close();
return true;
}
三、在XML文件中删除元素
函数名:DeleteXmlNode
参数:_path(传入xml文件生成路径)_name(传入要删除的元素名称) _isFull(是否删除全部元素)
功能:在指定位置的xml文件中删除指定元素
bool XmlOperation::DeleteXmlNode(QString _path, QString _name, bool _isFull)
{
if(_isFull == false)//删除部分元素
{
if(_path == "")
{
qDebug()<<"输入xml文件路径有误!";
return false;
}
QFile _file(_path);
if(!_file.open(QIODevice::ReadOnly))
{
qDebug()<<"Open "<<_path<<"failed!";
return false;
}
//移动光标到第0行
_file.seek(0);
//错误检测
QDomDocument _doc;
QString _error;
int _row;
int _column;
if(!_doc.setContent(&_file, &_error, &_row, &_column))
{
qDebug()<<_error<<_row<<_column;
return false;
}
_file.close();
//设置检测标志位
bool _bIsFind = false;
//获取根元素
QDomElement _root = _doc.documentElement();
//检索元素
QDomNode _firstNode = _root.firstChild();
while(!_firstNode.isNull() && _root.isElement())
{
QDomElement _node = _firstNode.toElement();
//元素名称
QString _ss = _node.nodeName();
if(_ss == _name)//名称匹配
{
_root.removeChild(_firstNode);
_bIsFind = true;
break;
}
//遍历该元素的兄弟元素
_firstNode = _firstNode.nextSibling();
}
if(!_file.open(QFile::WriteOnly|QFile::Truncate))
{
qDebug()<<"delete node faild!";
return false;
}
QTextStream outStream(&_file);
_doc.save(outStream,4);
_file.close();
if(_bIsFind)
return true;
else
return false;
}
else//删除全部子元素
{
if(_path == "")
{
qDebug()<<"输入xml文件路径有误!";
return false;
}
QFile _file(_path);
if(!_file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
qDebug()<<"Open "<<_path<<"failed!";
return false;
}
QDomDocument _doc;
//xml文件声明
QDomProcessingInstruction _instruction = _doc.createProcessingInstruction("xml","version=\'1.0\' encoding=\'utf-8\'");
_doc.appendChild(_instruction);
//创建根元素
QDomElement _root = _doc.createElement("root");
_doc.appendChild(_root);
//写入到文件
QTextStream outFile(&_file);
//缩进4格
_doc.save(outFile, 4);
_file.close();
return true;
}
}
四、在XML文件中修改元素值
函数名:ModifyXMlFile
参数:_path(传入xml文件生成路径)_pair(传入元素名称和对应值)
功能:在指定位置的xml文件中修改指定元素的值
bool XmlOperation::ModifyXMlFile(QString _path, QPair<QString,QString> _pair)
{
if(_path == "")
{
qDebug()<<"输入xml文件路径有误!";
return false;
}
QFile _file(_path);
if(!_file.open(QIODevice::ReadOnly))
{
qDebug()<<"Open "<<_path<<"failed!";
return false;
}
//移动光标到第0行
_file.seek(0);
//错误检测
QDomDocument _doc;
QString _error;
int _row;
int _column;
if(!_doc.setContent(&_file, &_error, &_row, &_column))
{
qDebug()<<_error<<_row<<_column;
return false;
}
_file.close();
//设置检测标志位
bool _bIsFind = false;
//获取根元素
QDomElement _root = _doc.documentElement();
//检索元素
QDomNode _firstNode = _root.firstChild();
while(!_firstNode.isNull() && _root.isElement())
{
QDomElement _node = _firstNode.toElement();
QString _text = _node.nodeName();
if(_pair.first == _text)//匹配元素名称
{
_firstNode.firstChild().setNodeValue(_pair.second);
QDomNode _newNode = _firstNode.firstChild();
_root.replaceChild(_node,_newNode);
_bIsFind = true;
break;
}
//遍历该元素的兄弟元素
_firstNode = _firstNode.nextSibling();
}
if(!_file.open(QFile::WriteOnly|QFile::Truncate))
{
qDebug()<<"modify node faild!";
return false;
}
QTextStream outStream(&_file);
_doc.save(outStream,4);
_file.close();
if(_bIsFind)
return true;
else
return false;
}
五、读取XML文件内容
函数名:ReadXmlFile
参数:_path(传入xml文件生成路径)
功能:读取指定位置的xml文件所有内容
QMap<QString, QString> XmlOperation::ReadXmlFile(QString _path)
{
//存储数据
QMap<QString, QString> _map;
if(_path == "")
{
qDebug()<<"输入xml文件路径有误!";
return _map;
}
QFile _file(_path);
if(!_file.open(QIODevice::ReadOnly))
{
qDebug()<<"Open "<<_path<<"failed!";
return _map;
}
//移动光标到第0行
_file.seek(0);
//错误检测
QDomDocument _doc;
QString _error;
int _row;
int _column;
if(!_doc.setContent(&_file, &_error, &_row, &_column))
{
qDebug()<<_error<<_row<<_column;
return _map;
}
_file.close();
//获取根元素
QDomElement _root = _doc.documentElement();
//检索元素
QDomNode _firstNode = _root.firstChild();
while(!_firstNode.isNull() && _root.isElement())
{
QDomElement _node = _firstNode.toElement();
QString _nodeName = _node.nodeName();
QString _nodeText = _node.toElement().text();
_map.insert(_nodeName,_nodeText);
_firstNode = _firstNode.nextSibling();
}
return _map;
}
六、查询XML文件中元素的值
函数名:FindXmlNode
参数:_path(传入xml文件生成路径)_name(传入要查询的元素名称)
功能:查询指定位置的xml文件中的指定元素的值
QString XmlOperation::FindXmlNode(QString _path, QString _name)
{
//存储元素值
QString _str = "";
if(_path == "")
{
qDebug()<<"输入xml文件路径有误!";
return _str;
}
QFile _file(_path);
if(!_file.open(QIODevice::ReadOnly))
{
qDebug()<<"Open "<<_path<<"failed!";
return _str;
}
//移动光标到第0行
_file.seek(0);
//错误检测
QDomDocument _doc;
QString _error;
int _row;
int _column;
if(!_doc.setContent(&_file, &_error, &_row, &_column))
{
qDebug()<<_error<<_row<<_column;
return _str;
}
_file.close();
//获取根元素
QDomElement _root = _doc.documentElement();
//检索元素
QDomNode _firstNode = _root.firstChild();
while(!_firstNode.isNull() && _root.isElement())
{
QDomElement _node = _firstNode.toElement();
QString _nodeName = _node.nodeName();
if(_nodeName == _name)
{
_str = _node.toElement().text();
break;
}
_firstNode = _firstNode.nextSibling();
}
return _str;
}
结尾
利用QDomDocument类解析XML文件操作的讲解就到这。上面只是对简单的XML文件的解析,至于复杂的XML文件解析,完全可以照葫芦画瓢:)