0

我是 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 和 LocB 的 tempid 的所有属性(模量、阿尔法和屈服强度)。

4

1 回答 1

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
}
于 2012-12-05T21:07:44.473 回答