通过Qt读取xml

时间:2012-12-05 18:53:32

标签: c++ xml qt

我是Qt和XML的新手。请帮我解决这个问题。我会对你很好。 这是我的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>

从上面的文件格式我必须提取Materialist(例如Material_A,Material_B,Material_C,Material_D),Temperaturelist(例如-65,70,300,400,1000)以及基于LocA的tempid的所有属性(Modulus,alpha和yieldstrength)和LocB。

1 个答案:

答案 0 :(得分:3)

QDomDocumentQXmlStreamReader是以“Qt方式”读取XML文档的两种主要方式。阅读文档以获取示例和说明。

就个人而言,我更喜欢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
}