How to get line number from XML file when using with QDomDocument?
-
wrote on 6 May 2020, 07:23 last edited by
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.
-
Lifetime Qt Championwrote on 6 May 2020, 07:35 last edited by mrjj 5 Jun 2020, 08:06
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 ?
-
wrote on 6 May 2020, 07:49 last edited by Bonnie 5 Jun 2020, 07:54
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. -
@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.
@nn26
Ok in that way. then i see why line numbers is important. -
@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.
-
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.wrote on 6 May 2020, 08:08 last edited by@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>
-
so you checked ?
Its not set for a Node unless there has been an error ? -
-
@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.
wrote on 6 May 2020, 09:21 last edited by@nn26 I think you've misunderstood something.
This is not theint *errorLine
inQDomDocument::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
-
@nn26 I think you've misunderstood something.
This is not theint *errorLine
inQDomDocument::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
wrote on 7 May 2020, 08:30 last edited by nn26 5 Jul 2020, 08:34@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.
-
@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.
-
@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.
wrote on 7 May 2020, 08:38 last edited by@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
Data
s -
@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
Data
swrote on 7 May 2020, 08:50 last edited by
1/16