Qt XmlQuery子查询属性

时间:2013-01-20 17:16:28

标签: c++ qt xquery qml

我正在尝试使用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找到解决这个问题的方法,但在此期间这只是有用的。

1 个答案:

答案 0 :(得分:0)

我不确定,因为我不知道QXmlQuery以及它如何处理查询解析和执行。但我想这可能是命名空间问题。在第一个查询中,您定义了一个默认命名空间,而在第二个查询中则没有。也许在第二个查询(childQuery)中不使用此默认命名空间。至少它适合@*/String()打印一些东西。也许您可以尝试@*:lat/string按预期工作。

否则,一些示例数据将非常有价值。