Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Modify an XML file QXMLStreamReader/Writer
Forum Updated to NodeBB v4.3 + New Features

Modify an XML file QXMLStreamReader/Writer

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Arqam
    wrote on 22 Aug 2017, 10:52 last edited by
    #1

    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
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 22 Aug 2017, 11:06 last edited by
      #2

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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Arqam
        wrote on 22 Aug 2017, 11:18 last edited by
        #3

        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;
        }
        
        1 Reply Last reply
        0

        1/3

        22 Aug 2017, 10:52

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved