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 and add contents into an XML file

Modify and add contents into an XML file

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 2.8k 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.
  • V Offline
    V Offline
    viniltc
    wrote on 15 Feb 2020, 19:14 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Feb 2020, 20:42 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 15 Feb 2020, 20:48 last edited by mrjj
        #3

        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>
        
        A 1 Reply Last reply 29 Jun 2022, 07:27
        3
        • V Offline
          V Offline
          viniltc
          wrote on 18 Feb 2020, 14:31 last edited by
          #4

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

          1 Reply Last reply
          0
          • M mrjj
            15 Feb 2020, 20:48

            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>
            
            A Offline
            A Offline
            Abhishek J S
            wrote on 29 Jun 2022, 07:27 last edited by
            #5

            @mrjj Thanks.helped.

            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