2012-12-05 77 views
0

我是Qt和XML的新手。請幫我解決這個問題。我會對你很好。 這裏是我的XML文件格式通過Qt讀取xml

< SiteSpecific> 

< SitesList>LocA;LocB;LocC< /SitesList> 

< LocA> 

     < MaterialList>Material_A;Material_B</MaterialList> 

     <Material Name="Material_A"> 

     <TemperatureList>-65;70;300;400;1000</TemperatureList> 

    <Density Value="0.286"/> 

    <PoissonRatio Value="0.27"/> 

     <Property tempid="01" temp="-65"> 
      <Modulus Value="32.77E+6"/> 
      <Alpha Value="8.15E-6"/> 
      <YieldStrength Value="33.90E+3"/> 
     </Property> 

     <Property tempid="02" temp="70"> 
      <Modulus Value="29.00E+6"/> 
      <Alpha Value="8.55E-6"/> 
      <YieldStrength Value="30.00E+3"/> 
=  </Property> 

     <Property tempid="03" temp="300"> 
      <Modulus Value="27.50E+6"/> 
      <Alpha Value="9.26E-6"/> 
      <YieldStrength Value="22.40E+3"/> 
     </Property> 

    </Material> 
</LocA> 
< LocB> 

     < MaterialList>Material_C;Material_D</MaterialList> 

     <Material Name="Material_C"> 

     <TemperatureList>-65;70;300;400;1000</TemperatureList> 

    <Density Value="0.286"/> 

    <PoissonRatio Value="0.27"/> 

     <Property tempid="01" temp="-65"> 
      <Modulus Value="32.77E+6"/> 
      <Alpha Value="8.15E-6"/> 
      <YieldStrength Value="33.90E+3"/> 
     </Property> 

    <Material Name="Material_D"> 

     <TemperatureList>-65;70;300;400;1000</TemperatureList> 

    <Density Value="0.286"/> 

    <PoissonRatio Value="0.27"/> 

     <Property tempid="01" temp="-65"> 
      <Modulus Value="32.77E+6"/> 
      <Alpha Value="8.15E-6"/> 
      <YieldStrength Value="33.90E+3"/> 
     </Property> 

    </Material> 
</LocB> 

從上面的文件格式,我要提取唯物主義(如Material_A,Material_B,Material_C,Material_D),Temperaturelist(如-65,70,300,400,1000)和所有屬性(基於LocA和LocB的tempid,模數,alpha和yieldstrength)。

+2

見http://doc.qt.digia.com/qt/xml-processing.html和HTTP ://doc.qt.digia.com/qt/examples-xml.html –

回答

3

QDomDocumentQXmlStreamReader是讀取XML文檔「Qt方式」的兩種主要方式。閱讀文檔中的示例和說明。

個人而言,我更喜歡QXmlStreamReader,但它確實有一條學習曲線。

編輯:這裏有一個小樣本代碼,沒有編譯,給你的總體思路:

//create some struct to store your data 
struct material_t 
{ 
    QString name; 
    QList<MatProp> properties; //a list of your temp-modulus-Alpha-Yield entries 
} 

QList<material_t> readLocation(QXmlStreamReader& xml) 
{ 
    QStringList matnames; 
    QList<material_t> matlist; 
    while(xml.readNextStartElement()) 
    { 
     if(xml.name() == "MaterialList") 
      matnames = xml.readElementText().split(";"); 
     else if(matnames.contains(xml.name().toString())) 
      matlist.append(readMaterial(xml));   //write your own reader that returns a material_t 
     else 
      xml.skipCurrentElement();     //you must skip every unwanted element 
    } 
    return matlist; 
} 

void readStuff(QXmlStreamReader& xml, QList<material_t>& mats) 
{ 
    while(xml.readNextStartElement()) 
    { 
     QStringList sitelist; 
     if(xml.name() == "SitesList") //key in on the XML node name 
     { 
      sitelist = xml.readElementText().split(";"); 
     } else if(sitelist.contains(xml.name().toString())) 
     { 
      mats.append(readLocation(xml)); 
     } else       //you have to skip every unwanted element 
      xml.skipCurrentElement(); 
    } 
} 

int main() 
{ 
    QList<material_t> materialist; 
    QFile file("your path here"); 
    if(file.open(QIODevice::Text | QIODevice::ReadOnly))  //the QIODevice must be open 
    { 
     QXmlStreamReader xml(&file); 
     readStuff(xml, materiallist); 
    } 
    //do stuff with materiallist 
} 
+0

非常感謝給予回覆兄弟.. – Kenta