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 set a node value using QDOM ?
Forum Updated to NodeBB v4.3 + New Features

How to set a node value using QDOM ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
4 Posts 2 Posters 2.6k 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.
  • K Offline
    K Offline
    kishore_hemmady
    wrote on 2 Mar 2018, 13:12 last edited by kishore_hemmady 3 Feb 2018, 13:13
    #1

    My XML looks something like this

    <main>
    <child1>
    <Content>Video</Content>
    <ZoneNumber>4</ZoneNumber>
    <Height>400</Height>
    <Width>600</Width>
    .
    .
    .
    </child1>
    .
    .
    .
    </main>

    I want to edit "height" tag's value from 400 to say 500 using DOM. I used node.setvalue("500") function for height tag its not working.Please suggest.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Paul Colby
      wrote on 2 Mar 2018, 19:14 last edited by
      #2

      Hi @kishore_hemmady,

      I want to edit "height" tag's value from 400 to say 500 using DOM

      You can do it a few ways, including:

      dom.elementsByTagName("Height").at(0).firstChild().setNodeValue("500");
      

      One thing that often catches people (well, caught me often in the past) is that the Height node does not contain the 400 text within the node itself, but rather the node has a child text node that contains the text, hence the firstChild() in the example above.

      To show it working:

          qDebug() << "== before ==";
          qDebug().noquote() << dom.toString();
          qDebug() << dom.elementsByTagName("Height").at(0).firstChild().nodeValue();
      
          dom.elementsByTagName("Height").at(0).firstChild().setNodeValue("500");
      
          qDebug() << "== after ==";
          qDebug() << dom.elementsByTagName("Height").at(0).firstChild().nodeValue();
          qDebug().noquote() << dom.toString();
      

      Output:

      == before ==
      <main>
       <child1>
        <Content>Video</Content>
        <ZoneNumber>4</ZoneNumber>
        <Height>400</Height>
        <Width>600</Width>
       </child1>
      </main>
      
      "400"
      == after ==
      "500"
      <main>
       <child1>
        <Content>Video</Content>
        <ZoneNumber>4</ZoneNumber>
        <Height>500</Height>
        <Width>600</Width>
       </child1>
      </main>
      

      Cheers.

      1 Reply Last reply
      3
      • K Offline
        K Offline
        kishore_hemmady
        wrote on 5 Mar 2018, 04:52 last edited by
        #3

        @Paul-Colby Hi,Thanks for the reply.
        the value is getting changed but its not getting written into the XML.I am able to print the height tag's first child node value before and after setting.But in my XML still the old value is present.

        P 1 Reply Last reply 5 Mar 2018, 07:20
        0
        • K kishore_hemmady
          5 Mar 2018, 04:52

          @Paul-Colby Hi,Thanks for the reply.
          the value is getting changed but its not getting written into the XML.I am able to print the height tag's first child node value before and after setting.But in my XML still the old value is present.

          P Offline
          P Offline
          Paul Colby
          wrote on 5 Mar 2018, 07:20 last edited by
          #4

          Hi @kishore_hemmady,

          its not getting written into the XML ... in my XML still the old value is present.

          I assume you mean its not getting written into the XML file. XML itself is a language, and DOM is an object model representing the data described by the language (in this case, parsed into RAM). If you the the changes to the DOM reflected back to an XML file somewhere, you need to write it out yourself. Its not hard to do though, for example:

              QFile file("file.xml");
          
              // Load the XML content from file into a DOM document.
              file.open(QFile::ReadOnly);
              QDomDocument dom;
              dom.setContent(&file);
              file.close();
          
              // Change the DOM in memory.
              dom.elementsByTagName("Height").at(0).firstChild().setNodeValue("500");
          
              // Write the DOM as XML content back to the file.
              file.open(QFile::WriteOnly);
              file.write(dom.toByteArray(1));
              file.close();
          

          Of course, you should do error checking, and probably backup the file before re-writing it.

          For the example above, a diff of the file.xml file before and after looks like:

          --- file.xml    2018-03-05 18:07:36.638367812 +1100
          +++ file.xml	2018-03-05 18:11:32.357453075 +1100
          @@ -2,7 +2,7 @@
            <child1>
             <Content>Video</Content>
             <ZoneNumber>4</ZoneNumber>
          -  <Height>400</Height>
          +  <Height>500</Height>
             <Width>600</Width>
            </child1>
           </main>
          

          Cheers.

          1 Reply Last reply
          3

          1/4

          2 Mar 2018, 13:12

          • Login

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