我正在处理我使用QXMLStreamReader
和QXMLStreamWriter
的内容,以便读取和写入文件。
但我无法找到一种可以修改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>
到一个EXISTING 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
答案 0 :(得分:2)
您可以使用#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;
}
来存储来自XML的内容。您可以创建新的子,标记,属性。
function range(start,end,step){
let countx = [];
if(typeof step==='undefined'){
step=1;
for(let start=1;start<=end;start+=step){
countx.push(start);
}
}
else {
for(let i=start;i>=end;i+=step)
countx.push(i);
}
return countx;
}
function sum(arrayx){
let total=0;
for(let i=0;i<=arrayx.length-1;i++){
total+= (+arrayx[i]);
}
return total;
}
console.log(sum(range(1,10)));