DOM 解析器中的 SetContent 错误

SetContent error in DOM Parser

本文关键字:SetContent 错误 DOM      更新时间:2023-10-16

我正在尝试读取和写入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);
}

当我的程序进入XML时,它没有任何错误,但是当它进入XML1和更远时,每次它进入

if( !inFile.open( QIODevice::ReadWrite | QIODevice::Text ) ) { qDebug( "Failed to open file for reading." ); return; }

我收到错误Failed to open file for reading..知道我做错了什么吗?

您尝试使用"读写"权限多次打开同一文件,而不在每个函数结束时关闭。

关闭 inFile 以释放每个 XMLn 函数末尾的 r/w 令牌。

inFile.close();

要么只用"读取"权限打开你的文件

if( !inFile.open( QIODevice::Read | QIODevice::Text ) )