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. Reading xml file using Qdomdocument, chlidnode count not coming correct
Forum Update on Tuesday, May 27th 2025

Reading xml file using Qdomdocument, chlidnode count not coming correct

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 715 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.
  • N Offline
    N Offline
    n-2204
    wrote on 23 Jul 2021, 05:26 last edited by
    #1

    Hi, i am reading xml file using Qdomdocument but I'm not able to read all the data, even its not showing ROW count correct.
    cubediagram then ROW as 1 and Name once it read upto a, for ROW childcount shows 1 only

    <cubediagram>
    <ROW>
    <Name>a</Name>
    <X>0</X>
    <Y>0</Y>
    </ROW>
    <ROW>
    <Name>b</Name>
    <X>1</X>
    <Y>4</Y>
    </ROW>
    </cubediagram>
    
    QDomElement root_element = xmlread.documentElement();   
    QString startTag = root_element.tagName();
    qDebug() << "The ROOT tag is" << startTag;
    int noofrows=  root_element.childNodes().count();
    
    QDomElement General = root_element.firstChild().toElement();
    QString data = General.tagName();
    qDebug() << "The FirstChild is" << data;
    int iCount = General.childNodes().size();
    int iCount2 = General.childNodes().count();
    
    QDomNodeList row_lst = root_element.elementsByTagName("ROW");
     int row=  row_lst.count();
    if (General.tagName() == "ROW")
    {
        QDomElement Component = General.firstChild().toElement();
        QString cmp = Component.tagName();
        qDebug() << "The data in Component Element is" << cmp;
        QString n;
    while (!Component.isNull())
        {
            if (Component.tagName() == "Name")
            {
                n = Component.firstChild().toText().data();
                qDebug() << "Name is:" << n;
            }
            if (Component.tagName() == "X")
            {
                n = Component.firstChild().toText().data();
                qDebug() << "X is:" << n;
            }
            if (Component.tagName() == "Y")
            {
                n = Component.firstChild().toText().data();
                qDebug() << "Y is:" << n;
            }
    		 Component = Component.nextSibling().toElement();
    	}
    }
    

    Plz tell where I'm doing wrong. So, I can read the file correctly.
    Thankyou

    J 1 Reply Last reply 23 Jul 2021, 08:37
    0
    • N n-2204
      23 Jul 2021, 05:26

      Hi, i am reading xml file using Qdomdocument but I'm not able to read all the data, even its not showing ROW count correct.
      cubediagram then ROW as 1 and Name once it read upto a, for ROW childcount shows 1 only

      <cubediagram>
      <ROW>
      <Name>a</Name>
      <X>0</X>
      <Y>0</Y>
      </ROW>
      <ROW>
      <Name>b</Name>
      <X>1</X>
      <Y>4</Y>
      </ROW>
      </cubediagram>
      
      QDomElement root_element = xmlread.documentElement();   
      QString startTag = root_element.tagName();
      qDebug() << "The ROOT tag is" << startTag;
      int noofrows=  root_element.childNodes().count();
      
      QDomElement General = root_element.firstChild().toElement();
      QString data = General.tagName();
      qDebug() << "The FirstChild is" << data;
      int iCount = General.childNodes().size();
      int iCount2 = General.childNodes().count();
      
      QDomNodeList row_lst = root_element.elementsByTagName("ROW");
       int row=  row_lst.count();
      if (General.tagName() == "ROW")
      {
          QDomElement Component = General.firstChild().toElement();
          QString cmp = Component.tagName();
          qDebug() << "The data in Component Element is" << cmp;
          QString n;
      while (!Component.isNull())
          {
              if (Component.tagName() == "Name")
              {
                  n = Component.firstChild().toText().data();
                  qDebug() << "Name is:" << n;
              }
              if (Component.tagName() == "X")
              {
                  n = Component.firstChild().toText().data();
                  qDebug() << "X is:" << n;
              }
              if (Component.tagName() == "Y")
              {
                  n = Component.firstChild().toText().data();
                  qDebug() << "Y is:" << n;
              }
      		 Component = Component.nextSibling().toElement();
      	}
      }
      

      Plz tell where I'm doing wrong. So, I can read the file correctly.
      Thankyou

      J Offline
      J Offline
      JonB
      wrote on 23 Jul 2021, 08:37 last edited by JonB
      #2

      @n-2204
      Scribbled in haste. firstChild() etc return QDomNodes, which (IIRC) return things like whitespace and comments. And when you call toElement() on that you are not checking whether it returns a QDomNode::isNull(). If you only want to look at QDomElements I think you should use calls like QDomNode::firstChildElement().

      1 Reply Last reply
      0
      • N Offline
        N Offline
        n-2204
        wrote on 23 Jul 2021, 09:10 last edited by
        #3

        @JonB ya count i got correct as element as C X then its not couting is i give firstChildElement() but when i save as X then its giving correct count of nodes.

        Component = Component.nextSibling().toElement();
        when m doing its not again going and reading x y 
        only reading Name 
        basically not iterating
        also i added loop to read all row 
        for (int i = 0; i < iCount2; i++) {
        //here if condition
        //here while loop
        }
        but only reading Name a ..
        
        J 1 Reply Last reply 23 Jul 2021, 09:53
        0
        • N n-2204
          23 Jul 2021, 09:10

          @JonB ya count i got correct as element as C X then its not couting is i give firstChildElement() but when i save as X then its giving correct count of nodes.

          Component = Component.nextSibling().toElement();
          when m doing its not again going and reading x y 
          only reading Name 
          basically not iterating
          also i added loop to read all row 
          for (int i = 0; i < iCount2; i++) {
          //here if condition
          //here while loop
          }
          but only reading Name a ..
          
          J Offline
          J Offline
          JonB
          wrote on 23 Jul 2021, 09:53 last edited by JonB
          #4

          @n-2204
          It may not be relevant to your issue. But I told you not to use calls like nextSibling() when there is nextSiblingElement(). And if you do and then call nextSibling().toElement() I told you you will need to check the return result and act on it, and you still do not.

          Change all your "node" calls to corresponding "element" calls.

          N 1 Reply Last reply 23 Jul 2021, 10:58
          0
          • J JonB
            23 Jul 2021, 09:53

            @n-2204
            It may not be relevant to your issue. But I told you not to use calls like nextSibling() when there is nextSiblingElement(). And if you do and then call nextSibling().toElement() I told you you will need to check the return result and act on it, and you still do not.

            Change all your "node" calls to corresponding "element" calls.

            N Offline
            N Offline
            n-2204
            wrote on 23 Jul 2021, 10:58 last edited by
            #5

            @JonB

            QDomNodeList n_list = xmlread.elementsByTagName("cubediagram");
            QDomElement n_el;
            if (n_list.isEmpty()) {
                QMessageBox::information(0, "Error!", "root element not found", 0);
            }
            n_el = n_list.at(0).toElement();
            
            QDomNodeList node_list = n_el.childNodes();
            int n_size = node_list.size();//noof rows
            
            QDomNodeList row_list = xmlread.elementsByTagName("ROW");
            QDomElement r_el;
            if (row_list.isEmpty()) {
                QMessageBox::information(0, "Error!", "child element not found", 0);
            }
            r_el = row_list.at(0).toElement();
            QDomNodeList rownode_list = r_el.childNodes();
            int r_size = rownode_list.size();  //noofcolumns
            

            using this way also not Giving correct count, when there is space LIKE C X

            <cubediagram>
            <ROW>
            <Name>a</Name>
             <C X>0</C X>
            <Y>0</Y>
            </ROW>
            
            J 1 Reply Last reply 23 Jul 2021, 12:12
            0
            • N n-2204
              23 Jul 2021, 10:58

              @JonB

              QDomNodeList n_list = xmlread.elementsByTagName("cubediagram");
              QDomElement n_el;
              if (n_list.isEmpty()) {
                  QMessageBox::information(0, "Error!", "root element not found", 0);
              }
              n_el = n_list.at(0).toElement();
              
              QDomNodeList node_list = n_el.childNodes();
              int n_size = node_list.size();//noof rows
              
              QDomNodeList row_list = xmlread.elementsByTagName("ROW");
              QDomElement r_el;
              if (row_list.isEmpty()) {
                  QMessageBox::information(0, "Error!", "child element not found", 0);
              }
              r_el = row_list.at(0).toElement();
              QDomNodeList rownode_list = r_el.childNodes();
              int r_size = rownode_list.size();  //noofcolumns
              

              using this way also not Giving correct count, when there is space LIKE C X

              <cubediagram>
              <ROW>
              <Name>a</Name>
               <C X>0</C X>
              <Y>0</Y>
              </ROW>
              
              J Offline
              J Offline
              JonB
              wrote on 23 Jul 2021, 12:12 last edited by JonB
              #6

              @n-2204 said in Reading xml file using Qdomdocument, chlidnode count not coming correct:

              <C X>

              This is not a legal XML node name! No wonder it misbehaves :) I don't know whether the Qt parser errors on it, skips it, or (just maybe?) transforms it and puts it in --- though your finding may indicate that is not the case....

              1 Reply Last reply
              2
              • N Offline
                N Offline
                n-2204
                wrote on 23 Jul 2021, 13:58 last edited by
                #7

                yes, i understood thanks

                1 Reply Last reply
                0

                1/7

                23 Jul 2021, 05:26

                • Login

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