¿Cómo puedo usar Qt QXmlQuery para ejecutar consultas XPath en un archivo local?

StackOverflow https://stackoverflow.com//questions/9675907

  •  12-12-2019
  •  | 
  •  

Pregunta

Estoy tratando de agarrar las coordenadas de .los archivos kml que ser algo como esto:

<?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>

Estoy tratando de utilizar QXmlQuery para recuperar las coordenadas con una cadena XPath similar a este:

kml/Document/Placemark[last()]/GeometryCollection/LineString/coordinates

Yo he probado y que aquí y confirmado que funciona, hasta ahora tan bueno.Pero he tenido un tiempo horrible llegar a trabajar en Qt.Yo he probado un montón de cosas, incluyendo sugerencias en otros posts de aquí ASÍ que, sin suerte.He aquí un par de ejemplos que muestran las variaciones:

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;
}

Puedo conseguir 0 y " \n" de cada una de ellas, respectivamente.A donde voy mal aquí y qué tengo que hacer para solucionarlo?Estoy abierto a otros enfoques como el tiempo que permanezca dentro del intervalo Qt (estoy usando 4.7) y el estándar de C++.

¿Fue útil?

Solución

Después de mucha experimentación, parece que he dado con la receta mágica:

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();
}

Que me da a mi cadena de ceros (o lo que sea las coordenadas que puede ser).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top