//另存为
void SaveNewFile(QString srcFileName,QString tarFileName){
QFile srcFile(srcFileName);
srcFile.open(QFile::ReadOnly | QFile::Text);
QFile tarFile(tarFileName);
tarFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate);
QXmlStreamReader reader(&srcFile);
QXmlStreamWriter writer(&tarFile);
writer.setAutoFormatting(true);
writer.writeStartDocument();
while (!reader.atEnd())
{
reader.readNext();
if(reader.isStartElement())
{
QXmlStreamAttributes attr = reader.attributes();
qDebug()<<reader.name().toString();
writer.writeStartElement(reader.name().toString());
if(attr.size()>0)
{
writer.writeAttributes(attr);
}
}
if(reader.isCharacters())
{
//qDebug()<<reader.text().toString();
writer.writeCharacters(reader.text().toString());
}
if(reader.isEndElement())
{
writer.writeEndElement();
}
}
writer.writeEndDocument();
srcFile.close();
tarFile.close();
}
void MainWindow::on_pushButton_2_clicked()
{
SaveNewFile("MapConfig.xml","testsave.xml");
}