初识---Qt解析XML文件(QDomDocument)
关于XML及其使用场景不在此多做介绍,今天主要介绍Qt中对于XML的解析。QtXml模块提供了一个读写XML文件的流,解析方法包含DOM和SAX,两者的区别是什么呢? DOM(Document Object Model):将XML文件保存为树的形式,操作简单,便于访问。SAX(Simple API for XML):接近于底层,速度较快,但不便于访问。
因为下vs2013下开发Qt有些库需要手动添加!!!QXml,QSql,,
一般出现这个问题都是库文件没有添加造成,这里使用QtNetwork就要加QtNetwork的库文件,在debug模式下需要加Qt5Networkd.lib库文件,在release模式下需要加QtNetwork5.lib库文件在哪里添加呢,一共有两个地方需要添加,缺一不可
1. 项目->属性->c/c++->常规->附加包含目录->在弹出的对话框中,点那个文件夹形状的按钮添加新行,输入$(QTDIR)\include\QtNetwork
2. 项目->属性->连接器->输入->附加依赖项,添加 Qt5Networkd.lib(debug模式)或者 Qt5Network.lib(release模式)
void ParseXML::parse(QString file_name)
{
if(file_name.isEmpty())
return; QFile file(file_name);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::information(NULL, QString("title"), QString("open error!")); return;
} QDomDocument document;
QString error;
int row = , column = ;
if(!document.setContent(&file, false, &error, &row, &column))
{
QMessageBox::information(NULL, QString("title"), QString("parse file failed at line row and column") + QString::number(row, ) + QString(",") + QString::number(column, )); return;
} if(document.isNull())
{
QMessageBox::information(NULL, QString("title"), QString("document is null!")); return;
} QDomElement root = document.documentElement(); //root_tag_name为persons
QString root_tag_name = root.tagName();
if(root.hasAttribute("name"))
{
//name为Qt
QString name = root.attributeNode("name").value();
} //获取id="1"的节点
QDomElement person = root.firstChildElement();
if(person.isNull())
return; QString person_tag_name = person.tagName(); //id为1
QString id = person.attributeNode("id").value(); //获取子节点,数目为2
QDomNodeList list = root.childNodes();
int count = list.count();
for(int i=; i
{
QDomNode dom_node = list.item(i);
QDomElement element = dom_node.toElement(); //获取id值,等价
QString id_1 = element.attributeNode("id").value();
QString id_2 = element.attribute("id"); //获取子节点,数目为4,包括:name、age、email、website
QDomNodeList child_list = element.childNodes();
int child_count = child_list.count();
for(int j=; j
{
QDomNode child_dom_node = child_list.item(j);
QDomElement child_element = child_dom_node.toElement();
QString child_tag_name = child_element.tagName();
QString child__tag_value = child_element.text();
}
} //按照name、age、email、website的顺序获取值
QDomElement element = person.firstChildElement();
while(!element.isNull())
{
QString tag_name = element.tagName();
QString tag_value = element.text();
element = element.nextSiblingElement();
}
}
XML文件如下所示:
<?xml version="1.0" encoding="GBK"?><Catalog name = "树形目录"> <View id = "default"> <任务年度/> <任务编号/> <任务名称/> </View> <View id = "1"> <任务名称/> <任务年度/> <任务编号/> </View> <View id = "2"> <任务年度/> <任务名称/> <任务编号/> </View></Catalog> |
读文件:
if("" == fileName)
{
qDebug()<<"Filename is Null";
return;
}
QFile file(DirectorOf("xml").absoluteFilePath(fileName));
if(!file.open(QFile::ReadOnly | QFile::Text))
qDebug()<<"open file"<<fileName<<"failed, error:"<<file.errorString();
/*解析Dom节点*/
QDomDocument document;
QString strError;
int errLin = , errCol = ;
if( !document.setContent(&file, false, &strError, &errLin, &errCol) ) {
qDebug()<<"parse file failed at line"<<errLin<<",column"<<errCol<<","<<strError;
return;
}
if( document.isNull() ) {
qDebug()<<"document is null !";
return;
}
QDomElement root = document.documentElement();
qDebug()<<root.tagName();
QDomElement catalogs = root.firstChildElement();
if( catalogs.isNull() )
return;
else
qDebug()<<catalogs.tagName();
while(!catalogs.isNull())
{
QString tag = catalogs.attributeNode("id").value();
QStringList child;
QPair<QString,QStringList> pair;
for(int i = ;i < catalogs.childNodes().size();i++)
child<<catalogs.childNodes().at(i).nodeName();
pair.first = tag;
pair.second = child;
catalogList.append(pair);
catalogs = catalogs.nextSiblingElement();
}
file.close();
写入XML

QFile file(DirectorOf("xml").absoluteFilePath(xmlName));
if (!file.open(QFile::ReadOnly | QFile::Text))
return false;
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument doc;
if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
return false;
file.close();
QDomElement root = doc.documentElement();
if(root.tagName() != "Catalog")
return false;
QDomElement element = doc.createElement("View");
QDomAttr idAttr = doc.createAttribute("id");
element.setAttributeNode(idAttr);
element.setAttribute("id",typeName);
for(int i = 0;i < catalogs.size();i++)
{
QDomElement cataItem = doc.createElement(catalogs.at(i));
element.appendChild(cataItem);
}
root.appendChild(element);
/* QDomProcessingInstruction instruction;
instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"GBK\"");
doc.appendChild(instruction);*/
QFile f(DirectorOf("xml").absoluteFilePath(xmlName));
if(!f.open(QFile::WriteOnly | QFile::Text))
return false;
QTextStream out(&f);
doc.save(out,4);
f.close();
return true;

初识---Qt解析XML文件(QDomDocument)的更多相关文章
- Qt解析XML文件(QXmlStreamReader)
(2013-08-03 10:53:53) 转载▼ 如何使用QXmlStreamReader来解析格式良好的XML,Qt的文档中指出,它是一种更快.更方便的Qt自己的SAX解析器(QXml ...
- Qt解析xml
发现用 Qt 解析 xml 文件非常方便,下面是一个简单的解析 xml 文件的例子: #include <QtCore/QCoreApplication> #include <QDo ...
- Qt中使用DOM解析XML文件或者字符串(实例)
因为需要读取配置文件,我的配置文件采用xml:因此编写了使用qt读取xml文件内容的代码,xml文件如下: <?xml version="1.0" encoding=&quo ...
- java解析XML文件
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源 ...
- OAF_文件系列5_实现OAF解析XML文件javax.xml.parsers(案例)
20150729 Created By BaoXinjian
- QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QXmlStreamReader/QXmlStreamWriter实现Qt下xml文件读写 ...
- Android 解析XML文件和生成XML文件
解析XML文件 public static void initXML(Context context) { //can't create in /data/media/0 because permis ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- JAVA中使用DOM解析XML文件
XML是一种方便快捷高效的数据保存传输的格式,在JSON广泛使用之前,XML是服务器和客户端之间数据传输的主要方式.因此,需要使用各种方式,解析服务器传送过来的信息,以供使用者查看. JAVA作为一种 ...
随机推荐
- Residual Networks <2015 ICCV, ImageNet 图像分类Top1>
本文介绍一下2015 ImageNet中分类任务的冠军——MSRA何凯明团队的Residual Networks.实际上,MSRA是今年Imagenet的大赢家,不单在分类任务,MSRA还用resid ...
- ACM数据结构相关资料整理【未完成,待补充】
在网上总是查不到很系统的练ACM需要学习的数据结构资料,于是参考看过的东西,自己整理了一份. 能力有限,欢迎大家指正补充. 分类主要参考<算法竞赛入门经典训练指南>(刘汝佳),山东大学数据 ...
- code manager tools TotoiseSVN安装及使用
TotoiseSVN安装及使用 TotoiseSVN官方下载地址:http://tortoisesvn.net/downloads.html TotoiseSVN安装:很简单,一路直下:就不在这说了, ...
- PHP中对数据库操作的封装
在动态网面设计中很多都要涉及到对数据库的操作,但是有时跟据需要而改用其它后台数据库,就需要大量修改程序.这是一件枯燥.费时而且容易出错的功作.其实我们可以用PHP中的类来实现对数据库操作的封装,从而使 ...
- [CF189A]Cut Ribbon(完全背包,DP)
题目链接:http://codeforces.com/problemset/problem/189/A 题意:给你长为n的绳子,每次只允许切a,b,c三种长度的段,问最多能切多少段.注意每一段都得是a ...
- IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)(转载)
继续说说ios不同版本之间的适配 先说一个东西:在xcode当中有一个东西叫targets,苹果的官方文档是这样说的: A target specifies a product to build an ...
- UVa 10250 The Other Two Trees
还是读了很长时间的题,不过题本身很简单. 可以把四棵树想象成正方形的四个顶点,已知两个相对顶点的坐标,求另外两个坐标. 不过,原题可没直接这么说,中间需要一些小证明. 题中说有一个平行四边形然后分别以 ...
- Volley : "参数param:{ inoutNo:inoutNo ,whcode:’’}
private void fuzzySearch() { mRequestQueue = Volley.newRequestQueue(getActivity()); String str = Sha ...
- memcached内存管理及key value长度限制
1)什么是内存碎片?内存是大小有限的资源.例如把内存比作一张小床,来了一个小伙伴,可以睡下,再来一个小伙伴也能睡下.现在两个人了,他们点了差不多的大小的位置(资源),位置还有剩下.然后再来一个小胖子, ...
- BZOJ 1123 BLO
tarjan求割点计算答案.注意不是每一棵子树都算答案.开个变量记一下. #include<iostream> #include<cstdio> #include<cst ...