PDA

View Full Version : XML File reading issue



yogesh
3rd March 2009, 11:28
Hi,
I am trying to read an xml file using DOM APIs, now there is an API in QDomElement elementsByTagName() which takes the element tag and returns the list of QDomNode as QDomNodeList. But I am not getting any Node when I am passing the QDomDocument which is having the mentioned element tag.It is always returning empty NodeList. Is there any kind of bug in this API or my usage is wrong.
Also if I have to locate an element in an xml tree is there any better way, I found this API to be qute simple to get the QDomElement having the requried element tag so I thought of using it.
Is there any other way as to how I can retrieve the element which is deep down in the tree??

Regards,
Yogesh

Lykurg
3rd March 2009, 11:32
Is there any kind of bug in this API
no, don't think so.

or my usage is wrong.
Guess so, but without providing your code, hard to say!


Is there any other way as to how I can retrieve the element which is deep down in the tree??

Well, I think elementsByTagName() is your friend... (or you could use XQuery)

yogesh
3rd March 2009, 13:00
QDomDocument m_Doc;
QFile file("input.xml");
file.open(QIODevice::ReadOnly)
byteArray = file.readAll();
m_Doc.setContent(byteArray);
QDomNode root = m_Doc.documentElement();
QDomNodeList nodeList = root.elementsByTagName(aNodeElement);
if(!nodeList.count())
{
QDomNode node = nodeList.at(0);
if(node.isElement())
{
QDomElement element = node.toElement();
aValue = element.text();
qDebug()<< aValue;
}
}

Can you please tell if there is any problem in the usage??

Regards,
Yogesh

Lykurg
3rd March 2009, 13:09
Hi,

so far I can't see any error but

is the file opened and the content set properly?
what's aNodeElement exactly? Right value?

yogesh
3rd March 2009, 13:15
Hi,
The file is opened properly and aNodeElement is one of the elements present in the xml file.I checked it in the debugging, still the NodeList is returning empty. Any idea why??

Regards,
Yogesh

talk2amulya
3rd March 2009, 13:18
is the code compiling? i see a semi-colon missing on the third line..

Lykurg
3rd March 2009, 13:25
So, to be sure: aNodeElement is a QString. Uses your XML file namespaces? That may be a problem.

Lykurg

Lykurg
3rd March 2009, 13:33
Äh, got it: nodeList.count() is no bool! use
if (nodeList.count() > 0) and your provided example won't compile because QDomNode has no elementsByTagName! So in future please check yourself your minimal code example to be runable...

Lykurg

yogesh
3rd March 2009, 15:46
Hi ,
Thanks a lot for the help. Surely next time I will keep in mind to put the working code actually the implementation was a bit complex with some other logic so I just picked up the requried code and pasted.
Thanks again.

Regards,
Yogesh