我正在尝试从看起来像这样的 .kml 文件中获取坐标:
<?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 字符串的坐标:
kml/Document/Placemark[last()]/GeometryCollection/LineString/coordinates
我已经在这里进行了测试并确认它有效,到目前为止一切都很好。但是我很难让它在 Qt 中工作。我已经尝试了很多东西,包括 SO 上其他帖子中的建议,但没有运气。这里有几个例子显示了变化:
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++ 中。