4
我試圖抓住從.KML文件看起來像這樣的座標:如何使用Qt的QXmlQuery在本地文件上運行XPath查詢?
<?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
我測試過here,並確認它的工作原理,迄今爲止都非常好。但是我在Qt中工作的時間很可怕。我已經嘗試了很多東西,包括其他職位上的建議,但沒有運氣。這裏的顯示變化的幾個例子:
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++中,我就可以接受其他方法。
謝謝,它幫助我將我的表達從xpath – xkrz 2012-04-30 14:23:30