I’m writing an application for working with GPX files and I’m having performance issues when using the QDomElement class to read large XML documents. Files with GPS paths containing thousands of waypoints can take half a minute to load.
This is my code for reading paths (routes or tracks):
void GPXPath::readXml(QDomElement &pathElement)
{
for (int i = 0; i < pathElement.childNodes().count(); ++i)
{
QDomElement child = pathElement.childNodes().item(i).toElement();
if (child.nodeName() == "trkpt" ||
child.nodeName() == "rtept")
{
GPXWaypoint wpt;
wpt.readXml(child);
waypoints_.append(wpt);
}
}
}
When analysing the code with Apple’s Instruments I noticed that QDomNodeListPrivate::createList() is responsible for most of the computing time and it is being called by both QDomNodeList::count() and QDomNodeList::item().
It appears that this is not an efficient way of iterating through the child elements of a QDomElement as the list seems to be re-generated for every operation. What approach should I use instead?
I tried this instead
Turns out it is a factor 15 faster. I might be able to do it even faster by using SAX, but this will do for now.