2013-10-11 43 views
1

解析當我們嘗試分析與QT的XML文檔中,我們使用類似:XML與QT

QString str; 
QXmlStreamAttributes attrib = xml_reader.attributes(); 
if(attrib.hasAttribute("id")) 
{ 
    str = attrib.value("id").toString(); 
} 

要做到這一點,我需要知道的屬性稱爲「ID」。有沒有辦法在不知道名稱的情況下閱讀第一個屬性?

在此先感謝。

+1

當你說「第一個屬性」你是什麼意思?屬性的順序沒有在XML中定義。因此,您必須確定使用特定條件的「第一個」 – AnatolyS

+0

QXmlStreamAttributes是一個向量。你應該能夠直接索引「第一個」QXMLStreamAttribute。 XML再次沒有指定屬性的順序。 – Ajith

回答

1

QXmlStreamAttributes類繼承QVector<QXmlStreamAttribute>。這意味着,你可以在「循環」的對象,就像你與QVector

做你應該能夠訪問的第一個項目:

attrib[0].name(); //containst a QStringRef to the name 
attrib[0].value(); //containst a QStringRef to the value 

通過強烈建議您檢查QVector方式大小第一;)

attrib.size();  //contains the number of attributes 
+1

或者,'attrib.begin() - > name()'(size check仍然適用) –