Question

I have a gpx file , basically that is a xml file. I want to read latitude and longitude values from that. Below i have posted the sample gpx file.

    <gpx>
    <wpt lon="80.0124" lat="13.125">
    </wpt>
    <wpt lon="80.0130" lat="13.124">
    </wpt>
    <wpt lon="80.0145" lat="13.122">
    </wpt>
    <wpt lon="80.0120" lat="13.121">
    </wpt>
    </gpx>

I need to get float values of latitude and longitude. Any help and suggestion is appreciated.

Thank you. Could i get the result without double quotes?

Was it helpful?

Solution

The key class here is QXmlStreamReader.

See the code below to get it work.

test.xml

<gpx>
<wpt lon="80.0124" lat="13.125">
</wpt>
<wpt lon="80.0130" lat="13.124">
</wpt>
<wpt lon="80.0145" lat="13.122">
</wpt>
<wpt lon="80.0120" lat="13.121">
</wpt>
</gpx>

main.cpp

#include <QXmlStreamReader>
#include <QDebug>
#include <QString>
#include <QFile>

int main()
{
    QFile file("test.xml");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "File open error:" << file.errorString();
        return 1;
    }
    QXmlStreamReader inputStream(&file);
    while (!inputStream.atEnd() && !inputStream.hasError())
    {
        inputStream.readNext();
        if (inputStream.isStartElement()) {
            QString name = inputStream.name().toString();
            if (name == "wpt")
                qDebug() << "lon:" << inputStream.attributes().value("lon").toFloat() << "lat:" << inputStream.attributes().value("lat").toFloat();
        }
    }
    return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

lon: 80.0124 lat: 13.125 
lon: 80.013 lat: 13.124 
lon: 80.0145 lat: 13.122 
lon: 80.012 lat: 13.121
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top