代码如下:
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("测试数据

这篇博客介绍了如何利用Qt库在C++中创建和生成XML文件,详细阐述了编码设置和文件写入过程,适用于需要处理XML数据的开发者。
最低0.47元/天 解锁文章
538

被折叠的 条评论
为什么被折叠?



