2013-01-20 1 views
1

QXmlQuery (Qt 5.0)를 사용하여 gpx 파일을 구문 분석하려고합니다.Qt XmlQuery 하위 쿼리 속성

생각이 있기 때문에,

문제는 그 속성 이름을 인식하지 것이다 ... 위도, 경도, 고도를 추출하기 위해 모든 trkpt 요소, 다음 하위 쿼리 각 요소를 수집하는 것입니다 이 일하지 않는 동안

query.setQuery 
     (
      "declare default element namespace \"http://www.topografix.com/GPX/1/1\";" 
      "declare variable $gpxFile external;" 
      "doc($gpxFile)//trkpt" 
     ); 

if(query.isValid()) 
{ 
    QXmlResultItems trkpts; 
    query.evaluateTo(&trkpts); 
    for(QXmlItem trkpt = trkpts.next(); !trkpt.isNull(); trkpt = trkpts.next()) 
    { 
     QXmlQuery childQuery; 
     QStringList res; 

     childQuery.setFocus(trkpt); 
     childQuery.setQuery 
       (
        "@*/string()" 
       ); // <------------------------- this prints out all the attributes 

     childQuery.evaluateTo(&res); 
     qDebug() << res << "\n"; 
    } 
} 

을 : 코드가 작동

 // ... same code as above 
     childQuery.setQuery 
       (
        "@lat/string()" 
       ); // <------------------------- this prints out only a \n 
     // ... same code as above 

잘못된 일에 대해 어떤 생각?

나는 모든 @lat는 "외부"쿼리를 사용하여 속성을 수집하려고하면 예상대로 작동하는지 참고 :

query.setQuery 
     (
      "declare default element namespace \"http://www.topografix.com/GPX/1/1\";" 
      "declare variable $gpxFile external;" 
      "doc($gpxFile)//trkpt/@lat/string()" 
     ); 

이것은 GPX 샘플 파일입니다

편집 :

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
<gpx 
    version="1.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.topografix.com/GPX/1/0" 
    xmlns:topografix="http://www.topografix.com/GPX/Private/TopoGrafix/0/2" 
    xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/2 http://www.topografix.com/GPX/Private/TopoGrafix/0/2/topografix.xsd"> 
    <trk> 
     <name>My track</name> 
     <desc>My track description</desc> 
     <trkseg> 
      <trkpt lat="38.919839863" lon="-121.020112049"> 
       <ele>265.447754</ele> 
       <time>2003-02-05T18:19:20Z</time> 
      </trkpt> 
      <trkpt lat="38.919796947" lon="-121.020240795"> 
       <ele>264.967041</ele> 
       <time>2003-02-05T18:19:20Z</time> 
      </trkpt> 
      <trkpt lat="38.919861320" lon="-121.020498287"> 
       <ele>263.044434</ele> 
       <time>2003-02-05T18:19:20Z</time> 
      </trkpt> 
      <trkpt lat="38.919990066" lon="-121.020798694"> 
       <ele>263.525146</ele> 
       <time>2003-02-05T18:19:20Z</time> 
      </trkpt> 
     </trkseg> 
    </trk> 
</gpx> 

편집

QXmlQuery 대신 QDomDocument를 사용하여이 문제의 해결 방법을 발견했습니다. 결과는 (간결의 위해 제거 모든 오류 검사) 아래의 코드처럼 보이는 :

QFile file = ("mytrack.gpx"); 
file.open(QIODevice::ReadOnly); 

QDomDocument doc("mydoc"); 
doc.setContent(&file); 

QDomElement root = doc.documentElement(); 
QDomNodeList trkpts = root.elementsByTagName("trkpt"); 

for(int i = 0; i < trkpts.length(); i++) 
{ 
    QDomElement e = trkpts.at(i).toElement(); 

    QString latitude = e.attribute("lat"); 
    QString longitude = e.attribute("lon"); 

    QDomNodeList elevation_list = e.elementsByTagName("ele"); 
    QString elevation = elevation_list.at(0).toElement().text(); 

    // ... and so on 
} 

내가 QXmlQuery를 사용하여이 문제에 대한 해결책을 찾기 위해 싶지만, 그 동안이 그냥 작동합니다.

답변

0

QXmlQuery 및 쿼리 구문 분석 및 실행을 처리하는 방법을 모르므로 잘 모르겠습니다. 하지만 네임 스페이스 문제 일 수 있다고 생각합니다. 첫 번째 쿼리에서는 기본 네임 스페이스를 정의하고 두 번째 쿼리에서는 그렇지 않습니다. 두 번째 쿼리 (childQuery)에서이 기본 네임 스페이스가 사용되지 않았을 수 있습니다. 적어도 @*/String()에 인쇄하는 것이 적합 할 것입니다. @*:lat/string이 예상대로 작동하면 시도해 볼 수 있습니다.

그렇지 않으면 일부 예제 데이터가 매우 유용합니다.

+0

나도 네임 스페이스 문제에 대해 생각해 보았지만 제안한 솔루션의 많은 변형이 작동하지 않는 것 같습니다! 제안에 따라 데이터 샘플을 추가 중이므로 도움이되기를 바랍니다. – Cristiano