Reading xml file using Qdomdocument, chlidnode count not coming correct
-
wrote on 23 Jul 2021, 05:26 last edited by
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 -
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.
Thankyouwrote on 23 Jul 2021, 08:37 last edited by JonB@n-2204
Scribbled in haste.firstChild()
etc returnQDomNode
s, which (IIRC) return things like whitespace and comments. And when you calltoElement()
on that you are not checking whether it returns aQDomNode::isNull()
. If you only want to look atQDomElement
s I think you should use calls likeQDomNode::firstChildElement()
. -
wrote on 23 Jul 2021, 09:10 last edited by
@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 ..
-
@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 ..
wrote on 23 Jul 2021, 09:53 last edited by JonB@n-2204
It may not be relevant to your issue. But I told you not to use calls likenextSibling()
when there isnextSiblingElement()
. And if you do and then callnextSibling().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-2204
It may not be relevant to your issue. But I told you not to use calls likenextSibling()
when there isnextSiblingElement()
. And if you do and then callnextSibling().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.
wrote on 23 Jul 2021, 10:58 last edited byQDomNodeList 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>
-
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>
wrote on 23 Jul 2021, 12:12 last edited by JonB@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....
-
wrote on 23 Jul 2021, 13:58 last edited by
yes, i understood thanks
1/7