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. How to modify value in existing XML element?
Forum Updated to NodeBB v4.3 + New Features

How to modify value in existing XML element?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.1k Views 1 Watching
  • 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
    AaronKelsey
    wrote on last edited by
    #1

    I have iterated over my XML document and I have been able to check whether the element exists. Unfortunately I am struggling to find the correct functions in the Qt documentation to then change the value of the element.

    This is the XML document I am loading and reading. I would like to change the string value of ItemB.

    <root>
        <item argtype="single" name="ItemA">
            <value type="uint32">123</value>
        </item>
    		
         <item argtype="single" name="ItemB">
            <value type="string">ChangeThisValueHere</value>
        </item>
    </root>
    

    I have accessed the node iterating over the XML document using this method.

    QDomElement qRoot = mqConfig.documentElement();
    
      if (!qRoot.isNull())
      {
        QDomNodeList qDomNodeList = qRoot.elementsByTagName("item");
    
        bool bItemB= false;
    
        for (int i = 0; i < qDomNodeList.size(); i++)
        {
          QDomElement domElement = qDomNodeList.at(i).toElement();
    
          if (domElement.attribute("name") == "ItemB")
          {
            // Modify Value "ChangeThisValueHere" 
    
            bCertExists = true;
          }
          else
          {
            bCertExists = false;
          }
        }
    
        if (!bCertExists)
        {
          createItem(qRoot, "SSL Certificate", "string", sCertFileName);
        }
      }
    

    Any help would be greatly appreciated

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You can just use
      https://doc.qt.io/qt-5/qdomelement.html#setAttribute
      to change a attributes value.
      Please notice that you need to save it back to file for the changes to be permanent.

      Update: Just noticed the "ChangeThisValueHere" which is not an attribute but node text / text node.

      A 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        You can just use
        https://doc.qt.io/qt-5/qdomelement.html#setAttribute
        to change a attributes value.
        Please notice that you need to save it back to file for the changes to be permanent.

        Update: Just noticed the "ChangeThisValueHere" which is not an attribute but node text / text node.

        A Offline
        A Offline
        AaronKelsey
        wrote on last edited by
        #3

        @mrjj so I'd simply write domElement.setAttribute("ItemB", ""newValueHere"); ?

        JonBJ 1 Reply Last reply
        0
        • A AaronKelsey

          @mrjj so I'd simply write domElement.setAttribute("ItemB", ""newValueHere"); ?

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

          @AaronKelsey
          No. And anyway I'm not sure that's right for what you are asking....

          I would like to change the string value of ItemB.

               <item argtype="single" name="ItemB">
                  <value type="string">ChangeThisValueHere</value>
              </item>
          

          So you want to alter ChangeThisValueHere? That is not an attribute. That is the (text) value of the <value> element.

          I can see @mrjj has taken your goal as to change the <value type="string"> to <value type="somethingelse">, which would be changing an attribute named type.

          A 1 Reply Last reply
          2
          • JonBJ JonB

            @AaronKelsey
            No. And anyway I'm not sure that's right for what you are asking....

            I would like to change the string value of ItemB.

                 <item argtype="single" name="ItemB">
                    <value type="string">ChangeThisValueHere</value>
                </item>
            

            So you want to alter ChangeThisValueHere? That is not an attribute. That is the (text) value of the <value> element.

            I can see @mrjj has taken your goal as to change the <value type="string"> to <value type="somethingelse">, which would be changing an attribute named type.

            A Offline
            A Offline
            AaronKelsey
            wrote on last edited by
            #5

            @JonB Yes you're right, I want to change the text value of the ItemB element.

            Would you be able to help me with this?

            mrjjM 1 Reply Last reply
            0
            • A AaronKelsey

              @JonB Yes you're right, I want to change the text value of the ItemB element.

              Would you be able to help me with this?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @AaronKelsey
              Hi
              Sorry first noticed the ChangeThisValueHere :( (thx @JonB)
              so setAttribute is not useful in this case.
              You want to change a text node/the node text
              Please see here
              https://stackoverflow.com/questions/6753782/edit-value-of-a-qdomelement

              1 Reply Last reply
              2

              • Login

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