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. Parse XML document and create/modify elements.
Forum Updated to NodeBB v4.3 + New Features

Parse XML document and create/modify elements.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.3k Views 2 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 am working on a project which requires parsing and modifying an XML document which is something I have not done before. I need to parse the XML document, check if the required 3 elements exist, if they don't I need to create them, otherwise if they do currently exist I just need to modify the value of it.

    The problem I am having is structuring multiple for loops and if statements to successfully accomplish this. I am aware that what I currently have is flawed as the size of the QDomNodeList is continually growing with size and is therefor stuck in an infinite loop.

    void InstallCert::modifyConfig()
    {
      loadConfig();
    
      QDomElement qRoot = mqConfig.documentElement();
    
      if (!qRoot.isNull())
      {
        QDomNodeList qDomNodeList = qRoot.elementsByTagName("item");
    
        for (int i = 0; i < qDomNodeList.size(); i++)
        {
          QDomElement domElement = qDomNodeList.at(i).toElement();
    
          if (domElement.attribute("name") == "SSL Certificate")
          {
            // Change value
          }
          else
          {
            createItem(qRoot, "SSL Certificate", "string", sCertFileName);
          }
    
          if (domElement.attribute("name") == "SSL Certificate Key")
          {
            // Change value
          }
          else
          {
            createItem(qRoot, "SSL Certificate Key", "string", sKeyFileName);
          }
    
          if (domElement.attribute("name") == "Webserver SSL")
          {
            // Change value
          }
          else
          {
            createItem(qRoot, "Webserver SSL", "bool", "true");
          }
        }
      }
    
      writeConfig();
    }
    

    Is anyone able to help me structure this so that I am able to achieve this?

    sierdzioS 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Where is the problem, exactly? You only have one for loop and no nested if statements in the code you posted.

      (Z(:^

      A 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Where is the problem, exactly? You only have one for loop and no nested if statements in the code you posted.

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

        @sierdzio Sorry I may not have been clear.

        I have an XML file that initially starts off with one item inside it, but it may in the future have more items within it (dynamic).
        Example:

        <root>
            <item>
                  ............
            </item>
        </root>
        

        When my function runs it needs to set 3 values within three items, it needs to parse the XML document and create the three items if they don't already exist, which will always happen on the first time. If the function is triggered for a second time then the XML should already have the 3 new items and it shouldn't have to create them again, but will be able to detect them and modify the values.

        End result:

        <root>
            <item A>
                  ............
            </item>
        
            <item B>
                  ............
            </item>
        
            <item C>
                  ............
            </item>
        
            <item D>
                  ............
            </item>
        </root>
        

        I need to try and create this as dynamically as possible as in the future more items may be added to the XML file.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          The code you wrote seems pretty well suited for this task, then.

          (Z(:^

          A 1 Reply Last reply
          0
          • sierdzioS sierdzio

            The code you wrote seems pretty well suited for this task, then.

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

            @sierdzio The current code that I have does not work. The first iteration takes the first item in the XML document and doesn't match true on any of the if statements, therefore creating all 3 items, but what if the XML document already had all four items in it? It would be creating them endlessly as the size of the DomNodeList grows in size.

            mrjjM 1 Reply Last reply
            0
            • A AaronKelsey

              I am working on a project which requires parsing and modifying an XML document which is something I have not done before. I need to parse the XML document, check if the required 3 elements exist, if they don't I need to create them, otherwise if they do currently exist I just need to modify the value of it.

              The problem I am having is structuring multiple for loops and if statements to successfully accomplish this. I am aware that what I currently have is flawed as the size of the QDomNodeList is continually growing with size and is therefor stuck in an infinite loop.

              void InstallCert::modifyConfig()
              {
                loadConfig();
              
                QDomElement qRoot = mqConfig.documentElement();
              
                if (!qRoot.isNull())
                {
                  QDomNodeList qDomNodeList = qRoot.elementsByTagName("item");
              
                  for (int i = 0; i < qDomNodeList.size(); i++)
                  {
                    QDomElement domElement = qDomNodeList.at(i).toElement();
              
                    if (domElement.attribute("name") == "SSL Certificate")
                    {
                      // Change value
                    }
                    else
                    {
                      createItem(qRoot, "SSL Certificate", "string", sCertFileName);
                    }
              
                    if (domElement.attribute("name") == "SSL Certificate Key")
                    {
                      // Change value
                    }
                    else
                    {
                      createItem(qRoot, "SSL Certificate Key", "string", sKeyFileName);
                    }
              
                    if (domElement.attribute("name") == "Webserver SSL")
                    {
                      // Change value
                    }
                    else
                    {
                      createItem(qRoot, "Webserver SSL", "bool", "true");
                    }
                  }
                }
              
                writeConfig();
              }
              

              Is anyone able to help me structure this so that I am able to achieve this?

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @AaronKelsey said in Parse XML document and create/modify elements.:

              QDomNodeList qDomNodeList = qRoot.elementsByTagName("item");

              You create a copy of the list. It will not grow while the loop is going.

              (Z(:^

              A 1 Reply Last reply
              1
              • A AaronKelsey

                @sierdzio The current code that I have does not work. The first iteration takes the first item in the XML document and doesn't match true on any of the if statements, therefore creating all 3 items, but what if the XML document already had all four items in it? It would be creating them endlessly as the size of the DomNodeList grows in size.

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

                @AaronKelsey
                Hi
                Why not see what you are reading with

                qDebug() << "item.name:" << domElement.attribute("name");
                before the ifs.
                that make its easier to guess why if statements do not trigger/are true
                The debugger is also good for that.

                1 Reply Last reply
                1
                • sierdzioS sierdzio

                  @AaronKelsey said in Parse XML document and create/modify elements.:

                  QDomNodeList qDomNodeList = qRoot.elementsByTagName("item");

                  You create a copy of the list. It will not grow while the loop is going.

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

                  @sierdzio Well when using the debugger that current code is continually creating items and does not end.

                  1 Reply Last reply
                  0
                  • sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    What does createItem() method do, then?

                    (Z(:^

                    A 1 Reply Last reply
                    0
                    • sierdzioS sierdzio

                      What does createItem() method do, then?

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

                      @sierdzio It creates an item and appends it to the root

                      void InstallCert::createItem(QDomElement& rqItem,
                                                   const QString& rsName,
                                                   const QString& rsType,
                                                   const QString& rsValue)
                      {
                        QDomElement qItem(mqConfig.createElement("item"));
                      
                        qItem.setAttribute("argtype", "single");
                        qItem.setAttribute("name", rsName);
                      
                        QDomElement qValue(mqConfig.createElement("value"));
                      
                        qValue.setAttribute("type", rsType);
                      
                        QDomText qText(mqConfig.createTextNode(rsValue));
                      
                        rqItem.appendChild(qItem);
                        qItem.appendChild(qValue);
                        qValue.appendChild(qText);
                      }
                      
                      1 Reply Last reply
                      0
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on last edited by
                        #11

                        @AaronKelsey said in Parse XML document and create/modify elements.:

                        mqConfig

                        What is mqConfig, then? My suspicion is that you are calling modifyConfig() indirectly somewhere, and that causes the apparent "loop".

                        (Z(:^

                        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