Problems with Open-Source Downloads read https://www.qt.io/blog/problem-with-open-source-downloads and https://forum.qt.io/post/638946

Modify and add contents into an XML file



  • Hi All,

    I'm modifying an XML file of following format:

    <!DOCTYPE newproject>
    <subject_details>
     <Name>Name5</Name>
     <Surname>Surname5</Surname>
     <Patient_ID>PID05</Patient_ID>
     <Date>01/01/2000 00:00</Date>
     <Clinician_Note>note note</Clinician_Note>
     <Settings>
      <Current>19</Current>
      <PW>25</PW>
      <Freq>26</Freq>
     </Settings>
    </subject_details>
    

    In order to add contents into the Settings node, I do the following steps as shown below.

    1. Open the XML file read-only, read it all in, close it.

    2. Make changes in-memory document.

    3. Then open the file for overwrite, write all content, close file.

    See code:

     //Open the file read-only, read it all in, close it.
         if(!file.open(QIODevice::ReadOnly  | QIODevice::Text))
            {     
                QMessageBox::information(this, "Unable to open file for read", file.errorString());
                return;
            }
    
            QDomDocument document;
            document.setContent(&file);
            QDomElement root = document.documentElement();
    
            file.close();
    
           // Make changes in-memory document.
    
            QDomElement newTag = document.createElement(QString("Settings"));
    
            QDomNode SettingsNode = root.elementsByTagName("Settings").at(0).firstChild();
            QDomElement SettingsNodeVal = SettingsNode.toElement();
    
            if (SettingsNodeVal.isNull())
            {
    
                QDomElement newCurrTag = document.createElement(QString("Current"));
                QDomText newCurrVal = document.createTextNode(currVal);
                newCurrTag.appendChild(newCurrVal);
                newTag.appendChild(newCurrTag);
    
                QDomElement newPWTag = document.createElement(QString("PW"));
                QDomText newPWVal = document.createTextNode(PWVal);
                newPWTag.appendChild(newPWVal);
                newTag.appendChild(newPWTag);
    
                QDomElement newFreqTag = document.createElement(QString("Freq"));
                QDomText newFreqVal = document.createTextNode(FreqVal);
                newFreqTag.appendChild(newFreqVal);
                newTag.appendChild(newFreqTag);
    
    
                root.appendChild(newTag);
            }
    
            //Then open the file for overwrite, write all content, close file.
    
            if(!file.open(QIODevice::WriteOnly  | QIODevice::Text))
            {
                QMessageBox::information(this, "Unable to open file for write", file.errorString());
                return;
            }
    
            QTextStream output(&file);
            output << document.toString();
            file.close();
    
            }
    

    With the code mentioned above, I can add Children to the Settings node if it is empty. This works fine.

    Can someone show me how to change those values in an existing Settings node?

    Thanks in advance


  • Lifetime Qt Champion

    Hi,

    Please take a look at the DOM Bookmarks Example..

    Basically you'll grad/modify a copy of the node and then replace the original with the new one.


  • Lifetime Qt Champion

    Hi
    For text nodes, you can use setNodeValue

          QDomElement root = document.documentElement();
          QDomNode SettingsNode = root.namedItem("Settings");
    
          QDomNode cur = SettingsNode.namedItem("Current");
          cur.firstChild().setNodeValue("val1"); // Note the extra FirstChild() its to get the INNER QDomText
    
          QDomNode pw = SettingsNode.namedItem("PW");
          pw.firstChild().setNodeValue("val2");
    
          QDomNode freq = SettingsNode.namedItem("Freq");
          freq.firstChild().setNodeValue("val3");
    

    result

    <subject_details>
     <Name>Name5</Name>
     <Surname>Surname5</Surname>
     <Patient_ID>PID05</Patient_ID>
     <Date>01/01/2000 00:00</Date>
     <Clinician_Note>note note</Clinician_Note>
     <Settings>
      <Current>val1</Current>
      <PW>val2</PW>
      <Freq>val3</Freq>
     </Settings>
    </subject_details>
    


  • @SGaist @mrjj Thanks a lot for your feedback :)


Log in to reply