Modify an XML file QXMLStreamReader/Writer



  • I am working on something where I am using QXMLStreamReader and QXMLStreamWriter, to read and write to a file.

    But am not able to find a way such that I can modify an XML, for example consider this XML :

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

    This is written using the following code :

    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();
    

    Now I want to add one more <LIGHT1> to an EXISTING xml file, then how is possible to do that in QT?

    I am doing the following:

    //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();
    

    And I am getting the following errors :

    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
    


  • It might be easier to use the xml module to achieve what you are looking for.



  • Following worked ::

    #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;
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.