我正在尝试读取和编写XML文件,我使用DOM
解析器来读取XML文件并使用xmlputget
来编写。
这是代码
void MainWindow::on_Save_button_clicked()
{
XML();
XML1();
XML2();
XML3();
XML4();
XML5();
}
void::MainWindow::XML()
{
QString path = ui->lineEdit_7->text();
QFile inFile(path );
if( !inFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for reading." );
}
QDomDocument dom;
if( !dom.setContent( &inFile ) )
{
qDebug( "Failed to parse the file into a DOM tree." );
inFile.close();
}
QDomElement docElem = dom.documentElement();
QDomNodeList node = docElem.elementsByTagName("ABC");
QDomNode parentNode = node.at(0).parentNode();
parentNode.removeChild(node.at(0));
QFile outFile(path);
if( !outFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for writing." );
}
QTextStream stream( &outFile );
stream << dom.toString();
outFile.close();
QXmlGet xmlget;
xmlget.load(path);
xmlget.findAndDescend("HEADER");
QXmlPut xmlput(xmlget);
xmlput.putString("ABC", "abc");
xmlput.save(path);
}
void MainWindow::XML1()
{
QString path = ui->lineEdit_7->text();
QFile inFile(path );
if( !inFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for reading." );
}
QDomDocument dom;
if( !dom.setContent( &inFile ) )
{
qDebug( "Failed to parse the file into a DOM tree." );
inFile.close();
}
QDomElement docElem = dom.documentElement();
QDomNodeList node = docElem.elementsByTagName("FILE");
QDomNode parentNode = node.at(0).parentNode();
parentNode.removeChild(node.at(0));
QFile outFile(path);
if( !outFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for writing." );
}
QTextStream stream( &outFile );
stream << dom.toString();
outFile.close();
QXmlGet xmlget;
xmlget.load(path);
xmlget.findAndDescend("HEADER");
QXmlPut xmlput(xmlget);
xmlput.putString("FIle", "file");
xmlput.save(path);
}
void MainWindow::XML2()
{
QString path = ui->lineEdit_7->text();
QFile inFile(path);
if( !inFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for reading." );
}
QDomDocument dom;
if( !dom.setContent( &inFile ) )
{
qDebug( "Failed to parse the file into a DOM tree." );
inFile.close();
return;
}
QDomElement docElem = dom.documentElement();
QDomNodeList node = docElem.elementsByTagName("Main");
QDomNode parentNode = node.at(0).parentNode();
parentNode.removeChild(node.at(0));
QFile outFile(path);
if( !outFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for writing." );
}
QTextStream stream( &outFile );
stream << dom.toString();
outFile.close();
QXmlGet xmlget;
xmlget.load(path);
xmlget.findAndDescend("HEADER");
QXmlPut xmlput(xmlget);
xmlput.putString("main", "main");
xmlput.save(path);
}
}
void MainWindow::XML3()
{
QString path = ui->lineEdit_7->text();
QFile inFile(path );
if( !inFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for reading." );
}
QDomDocument dom;
if( !dom.setContent( &inFile ) )
{
qDebug( "Failed to parse the file into a DOM tree." );
inFile.close();
return;
}
QDomElement docElem = dom.documentElement();
QDomNodeList node = docElem.elementsByTagName("Root");
QDomNode parentNode = node.at(0).parentNode();
parentNode.removeChild(node.at(0));
QFile outFile(path);
if( !outFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for writing." );
}
QTextStream stream( &outFile );
stream << dom.toString();
outFile.close();
QXmlGet xmlget;
xmlget.load(path);
xmlget.findAndDescend("HEADER");
QXmlPut xmlput(xmlget);
xmlput.putString("Root", "root");
xmlput.save(path);
}
&#13;
当我的程序转到XML
时,它没有任何错误,但是当它转到XML1
时,每次进入
if( !inFile.open( QIODevice::ReadWrite | QIODevice::Text ) )
{
qDebug( "Failed to open file for reading." );
return;
}
我收到错误Failed to open file for reading.
。知道我做错了什么吗?
答案 0 :(得分:2)
您尝试使用“读取和写入”权限多次打开同一个文件,而不会在每个函数结束时关闭。
关闭inFile以在每个XMLn函数结束时释放r / w标记。
inFile.close();
使用“read”右侧
打开文件if( !inFile.open( QIODevice::Read | QIODevice::Text ) )