Qt生成XML文件

前端之家收集整理的这篇文章主要介绍了Qt生成XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://blog.csdn.net/kusey/article/details/7241320

	QString filename = QFileDialog::getSaveFileName( this,"Save","","*.xml" );

	QFile file( filename );
	if( !file.open(QIODevice::WriteOnly | QIODevice::Text) )
	{
		return;
	}

	QDomDocument document;

	QString strHeader( "version=\"1.0\" encoding=\"UTF-8\"" );
	document.appendChild( document.createProcessingInstruction("xml",strHeader) );

	QDomElement	root_elem = document.createElement( "items" );
	root_elem.setAttribute( "id",1 );
	document.appendChild( root_elem );

	QDomElement item1 = document.createElement( "item" );
	item1.setAttribute( "src","<>" );
	item1.setAttribute( "dst","<>" );
	root_elem.appendChild( item1 );

	QDomElement item2 = document.createElement( "item" );
	item2.setAttribute( "src",""'&" );
	item2.setAttribute( "dst","\"'&" );
	root_elem.appendChild( item2 );

	QDomElement item3 = document.createElement( "item" );
	item3.setAttribute( "src",tr("测试数据") );
	item3.setAttribute( "dst",tr("一二三四") );
	root_elem.appendChild( item3 );

	QTextStream out( &file );
	document.save( out,4 );
	file.close();

生成的xml的格式如下:

<?xml version="1.0" encoding="UTF-8"?>

<items id="1">

<item src="&amp" dst="&it"/>

<item src="&amp" dst="&it"/>

<item src="测试数据" dst="一二三四"/>

</items>

原文链接:https://www.f2er.com/xml/300440.html

猜你在找的XML相关文章