c++ - 通过Qt读取xml

标签 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。

最佳答案

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
}

关于c++ - 通过Qt读取xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13730361/

相关文章:

c++ - 在 Objective-C(不是 iPhone)中编译 Eigen 时出现问题

c++ - 传递对对象的引用有什么好处?

c++ - 在变量中存储异构类类型

c++ - 运行函数并保持循环运行c++

xml - xml 命名空间中的 uri 规范

xml - XSLT:节点()?与节点()*

java - 使用 XSTREAM 将 XML 解析为 ArrayList

c++ - 显示对话框时如何禁用 Qt 应用程序中的用户交互?

c++ - Qwt 简单图示例

c++ - QT如何将Qlist传递给类构造函数