Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to modify value in existing XML element?



  • 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


  • Lifetime Qt Champion

    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.



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



  • @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.



  • @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?


  • Lifetime Qt Champion

    @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


Log in to reply