我一直在試圖從這個XML文件讀取節點文本值:提取與QXmlItem子節點的值作爲QXmlQuery重點
<!DOCTYPE structure>
<data>
<x>
<id>1</id>
<nam>tytuł</nam>
<tab>21</tab>
<ind>5</ind>
<pre>TY</pre>
<khw>C.TY</khw>
</x>
<x>
<id>2</id>
<nam>autor</nam>
<tab>21</tab>
<ind>5</ind>
<pre>FO</pre>
<khw>C.FO</khw>
</x>
<x>
<id>3</id>
<nam>hasło korporatywne</nam>
<tab>21</tab>
<ind>5</ind>
<pre>FN</pre>
<khw>C.FN</khw>
</x>
</data>
我想要做的就是獲取每一個節點,它是兒童和轉換它到QMap。我在獲取單個元素時沒有問題,但是當通過將QXmlQuery的結果設置爲焦點來獲取子項時,我計算子節點查詢的QString是空的。我使用這段代碼:
QXmlResultItems results;
QFile structure("./structure.xml"); // xml file, as described earlier
structure.open(QFile::ReadOnly);
QXmlQuery query;
query.setFocus(&structure);
query.setQuery("data/x");
query.evaluateTo(&results);
QXmlItem next = results.next();
while(!next.isNull()) {
qDebug() << next.toNodeModelIndex().stringValue(); // everything's fine. It prints contents of <x>'s child nodes
QXmlQuery childQuery;
QString r;
childQuery.setFocus(next);
childQuery.setQuery("./nam/text()"); // already tested: "/nam/text()", "/nam/string()", "x/nam/string()", "data/x/nam/string()" etc... still no luck.
childQuery.evaluateTo(&r);
qDebug() << r; // prints \n but it should print content of <nam> node.
next = results.next();
}
軟件我用:4.7.2 SDK直接從Qt的網站,在Windows和Linux 2.3.1 QtCreator QT(沒有這種特殊的情況下,任何的區別,結果都是一樣的) 。我想肯定這是我的知識缺乏的問題,而不是軟件錯誤,請大家幫忙
有趣的是,它與」 ./如果您將「nam」標記重命名爲「name」,那麼甚至可以使用「./name/string()」。 – alexisdm
看起來像這裏描述的相同的問題http://qt-project.org/forums/viewthread/25725 – CAMOBAP