Qt之XML解析与常见操作

Qt之XML解析与常见操作(通过DOM方式读写)

概述

  • QDomDocument类代表整个的XML文件。概念上讲:它是文档树的根节点,并提供了文档数据的基本访问方法。由于元素、文本节点、注释、指令执行等等不可能脱离一个文档的上下文,所以文档类也包含了需要用来创建这些对象的工厂方法。被创建的节点对象有一个ownerDocument()函数,它将对象与对象常见的文档上下文环境关联起来。DOM类中最常使用的是QDomNode、QDomDocument、QDomElement和QDomText,先简单的熟悉下其用法及操作。
1 在*.pro文件中添加 QT += xml
QT += xml
2 添加相关头文件
#include <QDomDocument>
#include <QFile>
#include <QIODevice>
3 读写操作

下面直接通过一个简单的示例进行了解Dom的结构及其操作

3.1 xml结构
<?xml version="1.0" encoding="utf-8"?>
	<Products id="1000" name="" type="">
		<Product id="0" name="">
			<childproduct name="" origin="" date="" address=""/>
			<childproduct name="" origin="" date="" address=""/>
			<childproduct name="" origin="" date="" address=""/>
		</Product>
		<Product id="1" name="">
			<childproduct name="" origin="" date="" address=""/>
			<childproduct name="" origin="" date="" address=""/>
		</Product>
		<Products id="1000" name="" type=""> ... </Product>
	</Products>
3.2 ReadXML

直接上代码

void ReadXML(QString sPathName, QMap<int, CProducts> maps)
{
	QFile file(sPathName);
	if (!file.open(QIODevice::ReadOnly))
	{
		return;
	}
	
	//定义一个dom文件
	QDomDocument docment;
	//setContent是将指定的内容指定给QDomDocument解析
	bool isok = document.setContent(&file);
	if (isok)
	{
		file.close();
		return;
	}
	file.close();
	
	QDomElement rootElement = documnet.documentElement();//返回根节点
	QDomNode  rootNode = rootElement.firstChild();//获取第一个Products节点
	CProducts cProcts;//用来保存数据
	while (!rootNode.isNull())
	{
		if (rootNode.isElement())
		{
			QDomElement productEle = rootNode.toElement();
			cProcts.product->id = productEle.attribute("id").toInt();
			cProcts.product->name = productEle.attribute("name");
			cProcts.product->type = productEle.attribute("type");

			// read Product node
			QDomNodeList productList = productEle.childNodes();
			CProduct cProduct;
			for (int i=0; i<productList.size(); ++i)
			{
				if (productList.at(i).isElement())
				{
					QDomElement childPros = productList.at(i).toElement();
					cProduct.id = childPros.attribute("id").toInt();
					cProduct.name = childPros.attribute("name");
					//read childproduct 
					QDomNodeList childList = childPros.childNodes();
					for (int j=0; j<childList.size(); ++j)
					{
						if (childList.at(i).isElement())
						{
							QDomElement childPros = childList.at(j).toElement();
							... ...
						}
					}
				}
			}
		}
		rootNode = rootNode.nextSibling();// 读取下一个Products节点
	}
}
3.3 WriteXML
void WriteXML(QString sPathName)
{
	QFile file(sPathName);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text))
	{
		return;
	}

	QDomDocument domcument;
	// xml header
	QString strHeader("version=\"1.0\" encoding=\utf-8\"");
	domcument.appendChild(domcument.createProcessingInstruction("xml",strHeader));
	// root element
	QDomElement rootNode;
	rootNode = domcument.createElement("Products");
	domcument.appendChild(rootNode);

	// Products element
	QMap<int, CProducts>::iterator iter = maps.begin();
	for (;iter != maps.end(); ++iter)
	{
		if (iter.value().CProduct.size() > 0)
		{
			QDomElement ele = domcument.createElement("Product");
			ele.setAttribute("id", iter.value().CProduct->id);
			ele.setAttribute("name", iter.value().CProduct->name);
			// Product element			
			... ...
		}
	}
	QTextStream out(&file);
	document.save(out, 4);
	out.reset();
	file.flush();
	file.close();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kevin_org

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值