DOM 解析器中的 SetContent 错误
SetContent error in DOM Parser
我正在尝试读取和写入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 ) )
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- DOM 解析器中的 SetContent 错误