最近项目中遇到了解析xml文件的问题,于是便想在网上搬砖来解决一下,找了一下,C++的大都是tinyxml库,qt的也是用document,都是解析小文件的xml。大文件的还不好找解析方式。于是就看到了用stream来解析,亲自试了一下,超级简单,方便。下面附上代码:
#include <QFile>
#include <QFileInfo>
#include <QXmlStreamReader>
#include <QDebug>
//SCD_VERSION_INFO为结构体
void HandleScdFile::parseXML(QString &full_name, SCD_VERSION_INFO& scd)
{
QFile file(full_name);
file.open(QIODevice::ReadOnly);
QXmlStreamReader reader(&file);
QString version, revision, when, who, what, why;
while (!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement()) {
if (reader.name() == "Hitem") {
version = reader.attributes().value("version").toString();
revision = reader.attributes().value("revision").toString();
when = reader.attributes().value("when").toString();
who = reader.attributes().value("who").toString();
what = reader.attributes().value("what").toString();
why = reader.attributes().value("why").toString();
}
}
}
qDebug() << version;
qDebug() << revision;
qDebug() << when;
qDebug() << who;
qDebug() << what;
qDebug() << why;
file.close();
strcpy(scd.scd_version, version.toLocal8Bit().data());
strcpy(scd.file_edit_reason, why.toLocal8Bit().data());
strcpy(scd.scd_file_editor, who.toLocal8Bit().data());
strcpy(scd.file_edit_time, when.toLocal8Bit().data());
}
下面附上我的部分xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<SCL xmlns="http://www.iec.ch/61850/2003/SCL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.1111" xmlns:naritech="http://www.111.cn" xmlns:sznari="http://www.sznari.com" xmlns:ext="http://www.111.sgcc.com.cn" xmlns:IEC_60870_5_104="http://www.sgepri.sgcc.com.cn" xmlns:csg="http://www.111.sgcc.com.cn">
<Private type="Substation virtual terminal conection CRC">2A357A78</Private>
<Header id="Default Substation" nameStructure="IEDName" version="2.9" revision="2.8" toolID="111">
<History>
<Hitem version="2.1" revision="1.0" when="2019-06-20 15:12:31" who="" what="" why=""/>
<Hitem version="1.1" revision="1.0" when="2019-07-17 14:43:51" who="" what="" why=""/>
<Hitem version="1.3" revision="1.0" when="2019-07-19 14:08:05" who="" what="" why=""/>
<Hitem version="1.3" revision="1.1" when="2019-07-20 10:03:18" who="" what="" why=""/>
<Hitem version="1.3" revision="1.2" when="2019-07-20 10:58:52" who="" what="" why=""/>
<Hitem version="1.4" revision="1.0" when="2019-07-20 11:19:29" who="" what="" why=""/>
<Hitem version="1.6" revision="1.0" when="2019-07-22 00:06:33" who="" what="" why=""/>
<Hitem version="1.6" revision="1.1" when="2019-07-22 10:01:59" who="" what="" why=""/>
<Hitem version="1.6" revision="1.2" when="2019-07-22 11:43:25" who="" what="" why=""/>
<Hitem version="1.7" revision="1.0" when="2019-07-22 12:35:03" who="" what="" why=""/>
<Hitem version="1.8" revision="1.0" when="2019-07-22 12:39:06" who="" what="" why=""/>
<Hitem version="1.8" revision="1.1" when="2019-07-22 12:58:40" who="" what="" why=""/>
<Hitem version="1.8" revision="1.2" when="2019-07-22 13:20:09" who="" what="" why=""/>
<Hitem version="1.9" revision="1.0" when="2019-07-22 14:11:47" who="" what="" why=""/>
<Hitem version="1.9" revision="1.1" when="2019-07-22 14:17:14" who="" what="" why=""/>
<Hitem version="1.9" revision="1.2" when="2019-07-22 15:55:32" who="" what="" why=""/>
<Hitem version="1.9" revision="1.3" when="2019-07-22 18:31:40" who="" what="" why=""/>
<Hitem version="1.9" revision="1.4" when="2019-07-22 22:44:37" who="" what="" why=""/>
<Hitem version="1.9" revision="1.5" when="2019-07-23 13:17:14" who="" what="" why=""/>
<Hitem version="2.0" revision="1.0" when="2019-07-23 13:34:41" who="" what="" why=""/>
<Hitem version="2.0" revision="1.1" when="2019-07-23 16:13:44" who="" what="" why=""/>
<Hitem version="2.0" revision="1.2" when="2019-07-23 16:41:28" who="" what="" why=""/>
<Hitem version="2.0" revision="1.3" when="2019-07-23 17:15:46" who="" what="" why=""/>
<Hitem version="2.0" revision="1.4" when="2019-07-23 18:37:02" who="" what="" why=""/>
<Hitem version="2.0" revision="1.5" when="2019-07-23 20:29:04" who="" what="" why=""/>
<Hitem version="2.0" revision="1.6" when="2019-07-23 21:20:40" who="" what="" why=""/>
<Hitem version="2.0" revision="1.7" when="2019-07-23 21:40:01" who="" what="" why=""/>
<Hitem version="2.0" revision="1.8" when="2019-07-23 22:35:00" who="" what="" why=""/>
<Hitem version="2.0" revision="1.9" when="2019-07-24 10:48:29" who="" what="" why=""/>
<Hitem version="2.1" revision="1.0" when="2019-07-24 14:22:14" who="" what="" why=""/>
<Hitem version="2.2" revision="1.0" when="2019-07-24 14:41:32" who="" what="" why=""/>
<Hitem version="2.2" revision="1.1" when="2019-07-24 14:51:49" who="" what="" why=""/>
<Hitem version="2.2" revision="1.2" when="2019-07-24 15:05:05" who="" what="" why=""/>
<Hitem version="2.2" revision="1.3" when="2019-07-24 16:14:29" who="" what="" why=""/>
<Hitem version="2.2" revision="1.4" when="2019-07-25 09:39:46" who="" what="" why=""/>
<Hitem version="2.3" revision="1.0" when="2019-07-25 10:07:19" who="" what="" why=""/>
<Hitem version="2.6" revision="1.0" when="2019-07-26 20:32:59" who="" what="" why=""/>
<Hitem version="2.6" revision="1.1" when="2019-07-26 21:11:47" who="" what="" why=""/>
<Hitem version="2.7" revision="1.0" when="2019-07-27 09:45:36" who="" what="" why=""/>
<Hitem version="2.7" revision="1.1" when="2019-07-27 11:34:08" who="" what="" why=""/>
<Hitem version="2.9" revision="1.0" when="2019-07-30 16:06:18" who="" what="" why=""/>
<Hitem version="2.9" revision="1.1" when="2019-07-30 16:31:31" who="" what="" why=""/>
<Hitem version="2.9" revision="1.2" when="2019-07-30 17:25:24" who="" what="" why=""/>
<Hitem version="2.9" revision="1.3" when="2019-08-01 18:30:46" who="" what="" why=""/>
<Hitem version="2.9" revision="1.4" when="2019-08-03 21:36:20" who="" what="" why=""/>
<Hitem version="2.9" revision="1.5" when="2019-08-03 22:28:17" who="" what="" why=""/>
<Hitem version="2.9" revision="1.6" when="2019-08-03 23:28:04" who="" what="" why=""/>
<Hitem version="2.9" revision="1.7" when="2019-08-03 23:38:51" who="" what="" why=""/>
<Hitem version="2.9" revision="1.8" when="2019-08-04 12:11:23" who="" what="" why=""/>
<Hitem version="2.9" revision="1.9" when="2019-08-04 14:41:13" who="" what="" why=""/>
<Hitem version="2.9" revision="2.0" when="2019-08-04 14:53:41" who="" what="" why=""/>
<Hitem version="2.9" revision="2.1" when="2019-08-04 14:56:23" who="" what="" why=""/>
<Hitem version="2.9" revision="2.2" when="2019-08-04 15:06:03" who="" what="" why=""/>
<Hitem version="2.9" revision="2.3" when="2019-08-06 19:50:26" who="" what="" why=""/>
<Hitem version="2.9" revision="2.4" when="2019-08-06 20:14:40" who="" what="" why=""/>
<Hitem version="2.9" revision="2.5" when="2019-08-07 09:42:18" who="" what="" why=""/>
<Hitem version="2.9" revision="2.6" when="2019-08-07 09:46:56" who="" what="" why=""/>
<Hitem version="2.9" revision="2.7" when="2019-08-19 09:53:51" who="" what="" why=""/>
<Hitem version="2.9" revision="2.8" when="2020-02-13 12:23:04" who="qqq" what="save11" why="save11"/>
</History>
这个xml文件大约是7M。亲测,读完整个文件需要7-8左右。我读取的信息是
<Hitem version="2.9" revision="2.8" when="2020-02-13 12:23:04" who="qqq" what="save11" why="save11"/>
这一行的内容。是不是比tinyxml还简单,哈哈。