c++ - DOM 解析器中的 SetContent 错误

标签 c++ qt dom

我正在尝试读取和写入 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( "无法打开文件进行读取。"); 返回; }

我收到错误消息无法打开文件进行读取。。知道我做错了什么吗?

最佳答案

您正试图以“读写”权限多次打开同一个文件,而没有在每个函数结束时关闭。

要么关闭您的 inFile 以释放每个 XMLn 函数末尾的 r/w 标记。

inFile.close();

要么只用“读”权限打开你的文件

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

关于c++ - DOM 解析器中的 SetContent 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35333859/

相关文章:

javascript - Jquery index() 找不到我的元素

c++ - 如何清除用 C++ 编写的控制台应用程序中的屏幕?

c++ - C++ 中的 WriteLog( ... ) 函数

c++ - 在 QListWidget 中动态插入项目

qt - 从 .ui 文件生成 .h 和 .cpp

c++ - 在标题栏中添加一个按钮

javascript - 通过选择器获取动态元素

javascript - 暂时删除并稍后重新插入 DOM 元素?

c++ - 对 "vtable for Menu"的 undefined reference

c++ - CMakeList 设置 CMAKE_PREFIX_PATH