I am new to Qt and XML . Please help me on solving this . I shall be greatful to you . Here is my XML file format
< 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>
From the above file format i have to extract Materialist(eg Material_A , Material_B , Material_C , Material_D) , Temperaturelist(eg -65,70,300,400,1000) and all the properties (Modulus , alpha and yieldstrength )based on tempid for LocA and LocB .
QDomDocument and QXmlStreamReader are the two main ways to read XML documents the "Qt way." Read the documentation for examples and instructions.
Personally, I prefer QXmlStreamReader, but it does have a learning curve.
Edit: Here's a little sample code, not compiled, to give you the general idea:
//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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.