Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to update a node in xml using QStreamWritter class?

How to update a node in xml using QStreamWritter class?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
13 Posts 4 Posters 4.5k 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.
  • S Offline
    S Offline
    Sherlin N G
    wrote on last edited by
    #1

    I have an xml file in which i want to change the the attribute values of a node node.
    how can i do it using QXmlStreamWritter class,

    ex:if i want to change the value of <heading> from reminder to something without being appending the new node node to the xml file.

    <note>
    <to>Tove</to>
    <from>kishore</from>
    <heading>Reminder</heading>
    <body>hi!</body>
    </note>

    jsulmJ 1 Reply Last reply
    0
    • S Sherlin N G

      I have an xml file in which i want to change the the attribute values of a node node.
      how can i do it using QXmlStreamWritter class,

      ex:if i want to change the value of <heading> from reminder to something without being appending the new node node to the xml file.

      <note>
      <to>Tove</to>
      <from>kishore</from>
      <heading>Reminder</heading>
      <body>hi!</body>
      </note>

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @Sherlin-N-G I guess you mean QXmlStreamWritter?
      Like shown here: http://doc.qt.io/qt-5/qxmlstreamwriter.html ?

      QXmlStreamWriter stream(&output);
          stream.setAutoFormatting(true);
          stream.writeStartDocument();
          ...
          stream.writeStartElement("bookmark");
          stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE
          stream.writeTextElement("title", "Qt Project");
          stream.writeEndElement(); // bookmark
          ...
          stream.writeEndDocument();

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Sherlin N G
        wrote on last edited by
        #3

        hi @jsulm

        i think this will write the node again to the xml file,
        i want to change the values of existing node attributes.

        thank you for the reply

        jsulmJ 1 Reply Last reply
        0
        • S Sherlin N G

          hi @jsulm

          i think this will write the node again to the xml file,
          i want to change the values of existing node attributes.

          thank you for the reply

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Sherlin-N-G You can simply overwrite the whole XML document.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Sherlin-N-G You can simply overwrite the whole XML document.

            S Offline
            S Offline
            Sherlin N G
            wrote on last edited by
            #5

            @jsulm
            hi
            i have multiple note node inside my XML file , how can i add the set attribute value to particular node node.

            JonBJ 1 Reply Last reply
            0
            • S Sherlin N G

              @jsulm
              hi
              i have multiple note node inside my XML file , how can i add the set attribute value to particular node node.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Sherlin-N-G
              You have to (search the document to) select the particular node you want to alter. You can sit inserting/deleting/updating nodes in an XML document for as long as you like. Then you write the complete XML document back to file when you're finished. That's the normal/simplest way of doing things in a whole XML document. Do you have a particular reason for wanting to go down to the QXmlStreamWriter level?

              K 1 Reply Last reply
              0
              • S Offline
                S Offline
                Sherlin N G
                wrote on last edited by
                #7

                hi @JonB i tried with code in the document , i am confused with these 2 line of code

                stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE
                stream.writeTextElement("title", "Qt Project");

                No, i can use any class for over writing but i feel QXmlStreamWriter is easy to implement.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Sherlin-N-G
                  You have to (search the document to) select the particular node you want to alter. You can sit inserting/deleting/updating nodes in an XML document for as long as you like. Then you write the complete XML document back to file when you're finished. That's the normal/simplest way of doing things in a whole XML document. Do you have a particular reason for wanting to go down to the QXmlStreamWriter level?

                  K Offline
                  K Offline
                  kishore_hemmady
                  wrote on last edited by
                  #8

                  @JonB Hi
                  I dont think so you can read a node you want to alter and write simultaneously using DOM.

                  JonBJ 1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sherlin N G
                    wrote on last edited by
                    #9

                    this is my xml file in which i need to change the value of X_value from car to bus by the below code

                    <Settings>
                    <insideSetting>
                    <note>
                    <X_value>car</X_value>
                    <Y_value>bike</Y_value>
                    </note>
                    </insideSetting>
                    </Settings>

                    this is my snippet to update a node attribute X_value
                    void MainWindow::on_pushButton_2_clicked()
                    {

                      QString output("/something/newXml1.xml");
                      QXmlStreamWriter stream(&output);
                      stream.setAutoFormatting(true);
                      stream.writeStartDocument();
                      stream.writeStartElement("Note");
                      stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE
                      
                      stream.writeTextElement("X_value", "Qt Project");
                      stream.writeEndElement(); // Zone
                    
                     // stream.writeEndDocument();
                    

                    }

                    please help in this.

                    jsulmJ 1 Reply Last reply
                    0
                    • K kishore_hemmady

                      @JonB Hi
                      I dont think so you can read a node you want to alter and write simultaneously using DOM.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @kishore_hemmady said in How to update a node in xml using QStreamWritter class?:

                      @JonB Hi
                      I dont think so you can read a node you want to alter and write simultaneously using DOM.

                      I meant, read the whole doc into DOM, alter, write whole DOM back. For simplicity, personally I wouldn't be doing anything at the whole QXmlStreamWriter level. That's why I was asking whether the OP is wedded to that.

                      1 Reply Last reply
                      0
                      • S Sherlin N G

                        this is my xml file in which i need to change the value of X_value from car to bus by the below code

                        <Settings>
                        <insideSetting>
                        <note>
                        <X_value>car</X_value>
                        <Y_value>bike</Y_value>
                        </note>
                        </insideSetting>
                        </Settings>

                        this is my snippet to update a node attribute X_value
                        void MainWindow::on_pushButton_2_clicked()
                        {

                          QString output("/something/newXml1.xml");
                          QXmlStreamWriter stream(&output);
                          stream.setAutoFormatting(true);
                          stream.writeStartDocument();
                          stream.writeStartElement("Note");
                          stream.writeAttribute("href", "http://qt-project.org/"); // <----- HERE
                          
                          stream.writeTextElement("X_value", "Qt Project");
                          stream.writeEndElement(); // Zone
                        
                         // stream.writeEndDocument();
                        

                        }

                        please help in this.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Sherlin-N-G You can use http://doc.qt.io/qt-5/qdomdocument.html
                        Simply read your whole document, then alter it and write it back to the file.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          Sherlin N G
                          wrote on last edited by
                          #12

                          @jsulm

                          above code will not work for updating??
                          do i need to read from QDom and then write?

                          JonBJ 1 Reply Last reply
                          0
                          • S Sherlin N G

                            @jsulm

                            above code will not work for updating??
                            do i need to read from QDom and then write?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @Sherlin-N-G
                            I do think you need to confirm whether you are happy to use QDomDocument level, as I and @jsulm are suggesting, or whether you actively wish to use QXmlStreamWriter? Which is what I have been asking.

                            1 Reply Last reply
                            0

                            • Login

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