gpt4 book ai didi

c++ - 修改 XML 文件 QXMLStreamReader/Writer

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:06 27 4
gpt4 key购买 nike

我正在做我正在使用的东西 QXMLStreamReaderQXMLStreamWriter , 读取和写入文件。

但是我无法找到一种方法来修改 XML,例如考虑这个 XML:

<?xml version="1.0"?>
<LAMPS>
<LIGHT1>
<State>statevalue</State>
<Room>roomvalue</Room>
<Potencial>potencialvalue</Potencial>
</LIGHT1>
</LAMPS>

这是使用以下代码编写的:

QFile file(filename);
file.open(QIODevice::WriteOnly);

QXmlStreamWriter xmlWriter(&file);
xmlWriter.setAutoFormatting(true);
xmlWriter.writeStartDocument();

xmlWriter.writeStartElement("LAMPS");

xmlWriter.writeStartElement("LIGHT1");
xmlWriter.writeTextElement("State", "statevalue" );
xmlWriter.writeTextElement("Room", "roomvalue");
xmlWriter.writeTextElement("Potencial", "potencialvalue");
xmlWriter.writeEndElement();
xmlWriter.writeEndElement();
file.close();

现在我想再加一个<LIGHT1>到现有的 xml 文件,那么如何在 QT 中做到这一点?

我正在做以下事情:

     //filename = my file path
QFile file(filename);
file.open(QIODevice::ReadWrite);
QXmlStreamWriter xmlWriter(&file);
QXmlStreamReader xmlReader;
xmlReader.setDevice(&file);
xmlWriter.setAutoFormatting(true);
while(!xmlReader.atEnd()){
if(xmlReader.isStartDocument())
xmlWriter.writeStartDocument();
if(xmlReader.isStartElement()){
xmlWriter.writeStartElement(xmlReader.name().toString());
if(xmlReader.name()=="LAMPS"){
xmlWriter.writeStartElement("Arqam");
xmlWriter.writeTextElement("Arqam","Lucid Dreamer");
xmlWriter.writeEndElement();
}
}
if(xmlReader.isEndElement())
xmlWriter.writeEndElement();
if(xmlReader.isEndDocument())
xmlWriter.writeEndDocument();
xmlReader.readNext();
file.close();

我收到以下错误:

QIODevice::write (QFile, "/Users/arqam/Desktop/XMLGenrator/input/zzzz.xml"): device not open
QIODevice::read (QFile, "/Users/arqam/Desktop/XMLGenrator/input/zzzz.xml"): device not open

最佳答案

您可以使用 QDomDocument 来存储来自 XML 的内容。您可以创建新的子项、标签、属性。

#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QFile>
#include <QDomElement>
#include <QDomDocument>
#include <QDomText>
#include <QTextStream>

int main()
{
QFile file("test.xml");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "Failed to open file";
return 0;
}
QDomDocument document;
if (!document.setContent(&file))
{
qDebug() << "failed to parse file";
file.close();
return 0;
}

file.close();

QDomElement docEle = document.documentElement();
QDomNodeList elements = docEle.elementsByTagName("LAMPS");

QDomElement light1 = document.createElement( "LIGHT1" );
QDomElement state = document.createElement("State");
QDomText nextNode = document.createTextNode("State");
state.appendChild(nextNode);
light1.appendChild(state);
docEle.appendChild( light1 );

QFile outFile( "test-result.xml" );
if( !outFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
qDebug( "Failed to open file for writing." );
return 0;
}

QTextStream stream( &outFile );
stream << document.toString();

outFile.close();
return 0;
}

关于c++ - 修改 XML 文件 QXMLStreamReader/Writer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45814463/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com