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. How to get line number from XML file when using with QDomDocument?
Forum Update on Monday, May 27th 2025

How to get line number from XML file when using with QDomDocument?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 2.6k 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.
  • N Offline
    N Offline
    nn26
    wrote on last edited by
    #1

    Hello,

    suppose we have xml file like this,

    <string>
            <Data>
                  <Name>abc</Name>
                  <Number>123</Number>
                  <Address>kjl</Address>
            </Data>
            <Data>
                  <Name>zutr</Name>
                  <Number>897</Number>
                  <Address>bgfs</Address>
            </Data>
    </string>
    

    when using with QDomDocument (or after parsing into QDomElement, QDomNode)

    how to get line number of any tag like, e.x. <Address>kjl</Address> ?

    Thanks.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      I dont think you can. :/ see @Bonnie post
      Line numbers are mostly used for parsing errors
      but i have not seen it associated with actual
      object.

      Can i ask why you need line numbers ?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #3

        Maybe you could check this

        int QDomNode::lineNumber() const
        

        For nodes created by QDomDocument::setContent(), this function returns the line number in the XML document where the node was parsed. Otherwise, -1 is returned.
        This function was introduced in Qt 4.1.

        N 1 Reply Last reply
        3
        • N Offline
          N Offline
          nn26
          wrote on last edited by
          #4

          @mrjj I am comparing two XML files. After checking from root element to each child nodes by using QdomDocument, QDomElement, QDomNode in each loop ,

          I want to also print line number from which that difference came.

          mrjjM JonBJ 2 Replies Last reply
          1
          • N nn26

            @mrjj I am comparing two XML files. After checking from root element to each child nodes by using QdomDocument, QDomElement, QDomNode in each loop ,

            I want to also print line number from which that difference came.

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

            @nn26
            Ok in that way. then i see why line numbers is important.

            1 Reply Last reply
            0
            • N nn26

              @mrjj I am comparing two XML files. After checking from root element to each child nodes by using QdomDocument, QDomElement, QDomNode in each loop ,

              I want to also print line number from which that difference came.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @nn26
              @Bonnie 's solution is the way to go.

              N 1 Reply Last reply
              0
              • B Bonnie

                Maybe you could check this

                int QDomNode::lineNumber() const
                

                For nodes created by QDomDocument::setContent(), this function returns the line number in the XML document where the node was parsed. Otherwise, -1 is returned.
                This function was introduced in Qt 4.1.

                N Offline
                N Offline
                nn26
                wrote on last edited by
                #7

                @Bonnie It is not useful. It will only show parsing error. like this example, if <string> tag is not ended with </string>.

                <string>
                        <Data>
                              <Name>abc</Name>
                              <Number>123</Number>
                              <Address>kjl</Address>
                        </Data>
                        <Data>
                              <Name>zutr</Name>
                              <Number>897</Number>
                              <Address>bgfs</Address>
                        </Data>
                <string>
                
                1 Reply Last reply
                0
                • JonBJ JonB

                  @nn26
                  @Bonnie 's solution is the way to go.

                  N Offline
                  N Offline
                  nn26
                  wrote on last edited by
                  #8

                  @JonB no, it is only useful for checking one time parsing error.

                  mrjjM JonBJ 2 Replies Last reply
                  0
                  • N nn26

                    @JonB no, it is only useful for checking one time parsing error.

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

                    @nn26

                    so you checked ?
                    Its not set for a Node unless there has been an error ?

                    1 Reply Last reply
                    1
                    • N nn26

                      @JonB no, it is only useful for checking one time parsing error.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @nn26

                      It is not useful. It will only show parsing error.

                      That is not what the docs state. I don't see any indication in the source code indicating it is only set on errors. As per @mrjj, have you tried it?

                      N 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @nn26

                        It is not useful. It will only show parsing error.

                        That is not what the docs state. I don't see any indication in the source code indicating it is only set on errors. As per @mrjj, have you tried it?

                        N Offline
                        N Offline
                        nn26
                        wrote on last edited by
                        #11

                        @JonB yes I have tried it. As I said, It is completely different thing (lineNumber()) to check error, line number and column when parsing XML file with setContent(). Here, my requirement is different.

                        B 1 Reply Last reply
                        0
                        • N nn26

                          @JonB yes I have tried it. As I said, It is completely different thing (lineNumber()) to check error, line number and column when parsing XML file with setContent(). Here, my requirement is different.

                          B Offline
                          B Offline
                          Bonnie
                          wrote on last edited by
                          #12

                          @nn26 I think you've misunderstood something.
                          This is not the int *errorLine in QDomDocument::setContent function.
                          You see? when you have parse errors, you can't get a valid QDomNode...
                          I've tried your xml text in the top post, this function works well.

                              QDomDocument doc;
                              if(doc.setContent(text.toUtf8())) {
                                  auto element = doc.documentElement().firstChildElement();
                                  qDebug() << element.tagName() << element.lineNumber() << element.columnNumber() << endl;
                              }
                          

                          the output is

                          "Data" 2 14 
                          
                          N 1 Reply Last reply
                          4
                          • B Bonnie

                            @nn26 I think you've misunderstood something.
                            This is not the int *errorLine in QDomDocument::setContent function.
                            You see? when you have parse errors, you can't get a valid QDomNode...
                            I've tried your xml text in the top post, this function works well.

                                QDomDocument doc;
                                if(doc.setContent(text.toUtf8())) {
                                    auto element = doc.documentElement().firstChildElement();
                                    qDebug() << element.tagName() << element.lineNumber() << element.columnNumber() << endl;
                                }
                            

                            the output is

                            "Data" 2 14 
                            
                            N Offline
                            N Offline
                            nn26
                            wrote on last edited by nn26
                            #13

                            @Bonnie thank you for the answer.

                            doc.documentElement().firstChildElement()
                            

                            this will give output of <Data> in line 2, but how to get line number of another <Data> tag when working in loop?

                                   ```
                            

                            QDomDocument doc;
                            QDomElement root = doc.documentElement();

                            	  
                            
                                        
                            
                            for example when working with loop after getting root  from above code,
                            
                                  
                            
                                   
                            

                            QDomNode node = root.firstChild();

                                while (!node.isNull()) {
                            	   QDomElement element = node.toElement();
                                          **qDebug()<< root.toElement().firstChildElement().lineNumber();**
                                           
                            	   ....
                                           ....
                                         node = node.nextSibling();
                                         
                                  }
                            
                            
                            
                            
                            
                            so, Here root.toElement().firstChildElement().lineNumber() is giving correct line number in first iteration but for other iteration it is repeating same line number which is not correct.
                            JonBJ B 2 Replies Last reply
                            0
                            • N nn26

                              @Bonnie thank you for the answer.

                              doc.documentElement().firstChildElement()
                              

                              this will give output of <Data> in line 2, but how to get line number of another <Data> tag when working in loop?

                                     ```
                              

                              QDomDocument doc;
                              QDomElement root = doc.documentElement();

                              	  
                              
                                          
                              
                              for example when working with loop after getting root  from above code,
                              
                                    
                              
                                     
                              

                              QDomNode node = root.firstChild();

                                  while (!node.isNull()) {
                              	   QDomElement element = node.toElement();
                                            **qDebug()<< root.toElement().firstChildElement().lineNumber();**
                                             
                              	   ....
                                             ....
                                           node = node.nextSibling();
                                           
                                    }
                              
                              
                              
                              
                              
                              so, Here root.toElement().firstChildElement().lineNumber() is giving correct line number in first iteration but for other iteration it is repeating same line number which is not correct.
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #14

                              @nn26
                              You want to know about the line number of each node, not always of the root's first element, don't you? So node.lineNumber() (or element.lineNumber()).

                              1 Reply Last reply
                              2
                              • N nn26

                                @Bonnie thank you for the answer.

                                doc.documentElement().firstChildElement()
                                

                                this will give output of <Data> in line 2, but how to get line number of another <Data> tag when working in loop?

                                       ```
                                

                                QDomDocument doc;
                                QDomElement root = doc.documentElement();

                                	  
                                
                                            
                                
                                for example when working with loop after getting root  from above code,
                                
                                      
                                
                                       
                                

                                QDomNode node = root.firstChild();

                                    while (!node.isNull()) {
                                	   QDomElement element = node.toElement();
                                              **qDebug()<< root.toElement().firstChildElement().lineNumber();**
                                               
                                	   ....
                                               ....
                                             node = node.nextSibling();
                                             
                                      }
                                
                                
                                
                                
                                
                                so, Here root.toElement().firstChildElement().lineNumber() is giving correct line number in first iteration but for other iteration it is repeating same line number which is not correct.
                                B Offline
                                B Offline
                                Bonnie
                                wrote on last edited by
                                #15

                                @nn26 Seems your question is how to iterate.

                                auto element = doc.documentElement().firstChildElement();
                                while(!element.isNull()) {
                                    qDebug() << element.tagName() << element.lineNumber() << endl;
                                    element = element.nextSiblingElement();
                                }
                                

                                Here you can get all of the Datas

                                N 1 Reply Last reply
                                1
                                • B Bonnie

                                  @nn26 Seems your question is how to iterate.

                                  auto element = doc.documentElement().firstChildElement();
                                  while(!element.isNull()) {
                                      qDebug() << element.tagName() << element.lineNumber() << endl;
                                      element = element.nextSiblingElement();
                                  }
                                  

                                  Here you can get all of the Datas

                                  N Offline
                                  N Offline
                                  nn26
                                  wrote on last edited by
                                  #16

                                  @Bonnie @JonB @mrjj thanks for the answer. Now, I am getting correct line number with node.lineNumber(). I am doing things recurcively that's causing confusion. for root It can be obtained by doc.firstChildElement().lineNumber() and for each other child node, node.lineNumber().

                                  1 Reply Last reply
                                  1

                                  • Login

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