我正在尝试使用 QXmlQuery (Qt 5.0) 解析 gpx 文件。
这个想法是收集所有 trkpt 元素,然后子查询每个元素以提取纬度、经度、高度......
问题是它似乎无法识别属性名称,因为此代码有效:
query.setQuery
        (
            "declare default element namespace \"http://www.topografix.com/GPX/1/1\";"
            "declare variable $gpxFile external;"
            "doc($gpxFile)//trkpt"
        );
if(query.isValid())
{
    QXmlResultItems trkpts;
    query.evaluateTo(&trkpts);
    for(QXmlItem trkpt = trkpts.next(); !trkpt.isNull(); trkpt = trkpts.next())
    {
        QXmlQuery childQuery;
        QStringList res;
        childQuery.setFocus(trkpt);
        childQuery.setQuery
                (
                    "@*/string()"
                ); // <------------------------- this prints out all the attributes
        childQuery.evaluateTo(&res);
        qDebug() << res << "\n";
    }
}
而这个没有:
        // ... same code as above
        childQuery.setQuery
                (
                    "@lat/string()"
                ); // <------------------------- this prints out only a \n
        // ... same code as above
有什么想法吗?
请注意,如果我尝试使用“外部”查询收集所有 @lat 属性,它会按预期工作:
query.setQuery
        (
            "declare default element namespace \"http://www.topografix.com/GPX/1/1\";"
            "declare variable $gpxFile external;"
            "doc($gpxFile)//trkpt/@lat/string()"
        );
编辑
这是一个 gpx 示例文件:
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<gpx
    version="1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.topografix.com/GPX/1/0"
    xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/2"
    xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/2 http://www.topografix.com/GPX/Private/TopoGrafix/0/2/topografix.xsd">
    <trk>
        <name>My track</name>
        <desc>My track description</desc>
        <trkseg>
            <trkpt lat="38.919839863" lon="-121.020112049">
                <ele>265.447754</ele>
                <time>2003-02-05T18:19:20Z</time>
            </trkpt>
            <trkpt lat="38.919796947" lon="-121.020240795">
                <ele>264.967041</ele>
                <time>2003-02-05T18:19:20Z</time>
            </trkpt>
            <trkpt lat="38.919861320" lon="-121.020498287">
                <ele>263.044434</ele>
                <time>2003-02-05T18:19:20Z</time>
            </trkpt>
            <trkpt lat="38.919990066" lon="-121.020798694">
                <ele>263.525146</ele>
                <time>2003-02-05T18:19:20Z</time>
            </trkpt>
        </trkseg>
    </trk>
</gpx>
编辑
我通过使用 QDomDocument 而不是 QXmlQuery 找到了解决此问题的方法。结果类似于下面的代码(为简洁起见,删除了所有错误检查):
QFile file = ("mytrack.gpx");
file.open(QIODevice::ReadOnly);
QDomDocument doc("mydoc");
doc.setContent(&file);
QDomElement root = doc.documentElement();
QDomNodeList trkpts = root.elementsByTagName("trkpt");
for(int i = 0; i < trkpts.length(); i++)
{
    QDomElement e = trkpts.at(i).toElement();
    QString latitude = e.attribute("lat");
    QString longitude = e.attribute("lon");
    QDomNodeList elevation_list = e.elementsByTagName("ele");
    QString elevation = elevation_list.at(0).toElement().text();
    // ... and so on
}
我想通过使用 QXmlQuery 找到解决此问题的方法,但与此同时,这只是可行的。