Qt 读写 XML

前端之家收集整理的这篇文章主要介绍了Qt 读写 XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
bool MAS_ParseBase::setContent( QDomDocument& document )
{
	if ( !document.setContent( m_pFile,false ) )
	{
		return false;
	}
	m_pFile->close();
}


void MAS_ParseBase::save( QDomDocument& document )
{
	QTextStream out( m_pFile );
	out.setCodec( "UTF-8" );
	document.save( out,4 );
	m_pFile->close();
}


void MAS_ParseBase::getSubNode( QDomElement element,XMLNode& xml )
{
	// 循环记录子节点信息
	QDomNode firstNode = element.firstChild();
	while ( !firstNode.isNull() )
	{
		QDomElement firstElement = firstNode.toElement();

		XMLNode xmlFirst;
		xmlFirst.qstrTagName = firstElement.tagName();
		QDomNamedNodeMap nodeMap = firstElement.attributes();
		for ( int i=0; i<nodeMap.size(); i++ )
		{
			NodeAttribute attribute;
			attribute.qstrNodeName = nodeMap.item(i).nodeName();
			attribute.qstrNodeValue = nodeMap.item(i).nodeValue();
			xmlFirst.vtAttribute.push_back(attribute);
		}

		if ( !firstElement.firstChild().toElement().isNull() )
		{
			getSubNode( firstElement,xmlFirst );
		}
		else
		{
			xmlFirst.qstrValue = firstElement.toElement().text();
		}
		
		xml.vtSubNode.push_back( xmlFirst );

		firstNode = firstNode.nextSibling();
	}
}


void MAS_ParseBase::setSubNode( QDomDocument& document,QDomElement element,std::vector< XMLNode >& vtXML )
{
	for ( int i=0; i<vtXML.size(); i++ )
	{
		// 添加子节点属性信息
		QDomElement firstElement = document.createElement( vtXML.at(i).qstrTagName );
		for ( int m=0; m<vtXML.at(i).vtAttribute.size(); m++ )
		{
			firstElement.setAttribute( vtXML.at(i).vtAttribute.at(m).qstrNodeName,vtXML.at(i).vtAttribute.at(m).qstrNodeValue );
		}
		element.appendChild( firstElement );

		if ( vtXML.at(i).vtSubNode.size() > 0 )
		{
			setSubNode( document,firstElement,vtXML.at(i).vtSubNode );
		}
		else
		{
			firstElement.appendChild( document.createTextNode( vtXML.at(i).qstrValue ) );
		}
	}
}


XMLNode MAS_ParseXML::read()
{
	XMLNode xml;
	QDomDocument document;
	if ( !setContent(document) )
	{
		return xml;
	}

	QDomElement element = document.documentElement();

	// 记录根节点信息
	xml.qstrTagName = element.tagName();

	QDomNamedNodeMap nodeMap = element.attributes();
	for ( int i=0; i<nodeMap.size(); i++ )
	{
		NodeAttribute attribute;
		attribute.qstrNodeName = nodeMap.item(i).nodeName();
		attribute.qstrNodeValue = nodeMap.item(i).nodeValue();
		xml.vtAttribute.push_back(attribute);
	}

	// 递归获取子节点
	getSubNode( element,xml );

	return xml;
}

void MAS_ParseXML::write( XMLNode xml )
{
	// 添加文件头信息
	QDomDocument document;
	QDomProcessingInstruction instruction = document.createProcessingInstruction( "xml","version = \"1.0\" encoding = \"UTF-8\"" );
	document.appendChild( instruction );

	// 添加根节点
	QDomElement element = document.createElement( xml.qstrTagName );
	for ( int i=0; i<xml.vtAttribute.size(); i++ )
	{
		element.setAttribute( xml.vtAttribute.at(i).qstrNodeName,xml.vtAttribute.at(i).qstrNodeValue );
	}
	document.appendChild( element );

	// 递归写入子节点
	setSubNode( document,element,xml.vtSubNode );

	// 将数据写入到XML文件文件的每个空间为4个空格
	save( document );

	QMessageBox::information( NULL,QString::fromLocal8Bit("文件写入进度提示:"),QString::fromLocal8Bit("文件写入完毕!"),QMessageBox::Ok );
}


XML解析用到的结构体:
struct NodeAttribute
{
	QString qstrNodeName;
	QString qstrNodeValue;
};
struct XMLNode
{
	QString qstrTagName;
	QString qstrValue;
	std::vector< NodeAttribute > vtAttribute;
	std::vector< XMLNode > vtSubNode;
};
原文链接:https://www.f2er.com/xml/294643.html

猜你在找的XML相关文章