QT의 QXMLQUERY를 사용하여 로컬 파일에서 XPath 쿼리를 실행하려면 어떻게합니까?

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

  •  12-12-2019
  •  | 
  •  

문제

다음과 같이 보입니다 .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
.

나는 여기에서 를 확인하고, 지금까지는 그렇게 훨씬 좋은 것을 확인했습니다.그러나 나는 끔찍한 시간을 보내고 끔찍한 시간을 보내고 있습니다.운이없이 다른 게시물의 제안을 포함하여 많은 일을 시도했습니다.다음은 변형을 보여주는 몇 가지 예입니다.

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 ++.

도움이 되었습니까?

해결책

많은 실험 후에, 나는 마법의 조리법을 쳤다.

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

나에게 0의 끈이 똑같이 (또는 좌표가있을 수 있는지).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top