首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Qt的QXmlQuery在本地文件上运行XPath查询?

如何使用Qt的QXmlQuery在本地文件上运行XPath查询?
EN

Stack Overflow用户
提问于 2012-03-13 06:48:40
回答 1查看 5.7K关注 0票数 8

我正在尝试从类似如下的.kml文件中获取坐标:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
    <Document>
        <name>Name</name><Style id="roadStyle"><LineStyle><color>7fcf0064</color><width>6</width></LineStyle></Style><Snippet><![CDATA[<font size=+1><a href="http://example.com/">Printable view</a></font>]]></Snippet>
        <Placemark>
            <name>Example</name>
            <description><![CDATA[example]]></description><address>100 Example St</address><StyleMap><Pair><key>normal</key><Style><IconStyle><Icon><href>http://example.com</href></Icon><hotSpot x="0.000000" y="0.000000" xunits="fraction" yunits="fraction" /></IconStyle><ListStyle><ItemIcon><href>http://example.com</href></ItemIcon></ListStyle></Style></Pair><Pair><key>highlight</key><Style><IconStyle><scale>1.000000</scale><Icon><href>http://example.com</href></Icon><hotSpot x="0.000000" y="0.000000" xunits="fraction" yunits="fraction" /></IconStyle><ListStyle><ItemIcon><href>http://example.com</href></ItemIcon></ListStyle></Style></Pair></StyleMap><Point><coordinates>0.000000,0.000000,0</coordinates></Point><LookAt><longitude>0.000000</longitude><latitude>0.000000</latitude><range>100.000000</range><tilt>45.000000</tilt><heading>0.000000</heading></LookAt>
        </Placemark>
        <Placemark>
            <name>Route</name>
            <description><![CDATA[Example]]></description>
            <GeometryCollection>
                <LineString>
                    <coordinates>0.000000,0.000000,0.000000 0.000000,0.000000,0.000000 0.000000,0.000000,0.000000 0.000000,0.000000,0.000000 0.000000,0.000000,0.000000 0.000000,0.000000,0.000000 0.000000,0.000000,0.000000</coordinates>
                </LineString>
            </GeometryCollection>
            <styleUrl>#roadStyle</styleUrl>
        </Placemark>
    </Document>
</kml>

我尝试使用QXmlQuery通过类似于下面这样的XPath字符串检索坐标:

代码语言:javascript
复制
kml/Document/Placemark[last()]/GeometryCollection/LineString/coordinates

我已经测试了here,并确认它工作正常,到目前为止一切正常。但我在Qt上花了很长时间才让它正常工作。我尝试了很多东西,包括在SO上的其他帖子中的建议,但都没有成功。下面是几个示例,展示了各种变体:

代码语言:javascript
复制
void testQuery1(QString &filename) {
    QXmlQuery query;
    query.bindVariable("kmlFile", QVariant(filename));
    query.setQuery("declare default element namespace \"http://earth.google.com/kml/2.0\"; declare variable $kmlFile external; doc($kmlFile)//coordinates");

    QStringList results;
    query.evaluateTo(&results);

    qDebug() << results.size();
}

void testQuery2(QString &filename) {
    QFile file(filename);
    file.open(QIODevice::ReadOnly);

    QXmlQuery query;
    query.setFocus(&file);
    query.setQuery("kml/Document/Placemark[last()]/GeometryCollection/LineString/coordinates");

    QString result;
    query.evaluateTo(&result);

    qDebug() << result;
}

我分别从它们中的每一个获得0" \n"。我在哪里出了问题,我需要做些什么来修复它?我对其他方法持开放态度,只要它们停留在Qt (我使用4.7)和标准C++范围内。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-14 02:13:59

经过多次实验,我似乎发现了一个神奇的食谱:

代码语言:javascript
复制
void testQuery(QString &filename) {
    QFile file(filename);
    file.open(QIODevice::ReadOnly);

    QXmlQuery query;
    query.bindVariable("kmlFile", &file);
    query.setQuery("declare default element namespace \"http://earth.google.com/kml/2.0\"; declare variable $kmlFile external; doc($kmlFile)/kml/Document/Placemark[last()]/GeometryCollection/LineString/coordinates/text()");

    QString result;
    query.evaluateTo(&result);

    qDebug() << result;

    file.close();
}

这就给了我一串零(或者其他坐标)。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9675907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档