1

我想读取一个 XML 文件并将元素存储到对象的 QList 中。以下是我编写的部分代码。当我阅读时,它什么也没有存储。在控制台上,所有输出都是“”;我做得不好是什么?

XML 内容

<Cars>
   <Car>
     <Owner>
       <FirstName> John <FirstName/>
       <LastName> Smith <LastName/>
       <Address> Canada <Address/>
     <Owner/>
   <Car/>
........More data
........More cars
<Cars/>

xml读取代码

class Owner
{
private:
    QString firstName;
    QString lastName;
    QString address;
public:
    Owner();
    Owner(QString fName, QString lName, QString addr);
    QString getFirstName()const;
    QString getLastName() const;
    QString getAddress() const;
    void setFirstName(QString fName);
    void setLastName(QString lName);
    void setAddress(QString addr);
};

class Vehicle
{
private:
    Owner newOwner;
    QList<Verification> newVerification;
    Registration newRegistration;
    GeneralData newGeneralData;
    Equipment newEquipment;
    Characteristics newCharacteristics;

public:

    Vehicle();
    Owner getOwner() const;
    QList<Verification> getVerifications() const;
    int getVerificationsNumber() const;
    Registration getRegistration() const;
    GeneralData getGeneralData() const;
    Equipment getEquipment() const;
    Characteristics getCharacteristics() const;
    void setOwner(Owner owner);
    void setVerification(QList<Verification> ver);
    void setRegistration(Registration reg);
    void setGeneralData(GeneralData genData);
    void setEquipment(Equipment equipment);
    void setCharacteristics(Characteristics characteristics);
};


QList<Vehicle> readXML(QString fileName){
    QList<Vehicle> vehiclesList;
    QDomDocument document;
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "Failed to open file";
    } else {
        if(!document.setContent(&file)){
            qDebug() << "Failed to load document";
        } else {
            qDebug() << "Finished";
        }
        file.close();
    }

    QDomElement root = document.firstChildElement();
    QDomNodeList carElements = root.elementsByTagName("Car");
    for(int i = 0; i < carElements.count(); i++){
        //Car
        //****************************************************************
        //****************************************************************
        QDomNode carNode = carElements.at(i);

        //Owner
        //****************************************************************
        QDomElement ownerElement = carNode.firstChildElement("Owner");

        //FirstName
        QDomElement fNameElement = ownerElement.firstChildElement("FirstName");
        QString fName = fNameElement.text();
        qDebug() << fName; // it outputs " "


        //LastName
        QDomElement lNameElement = fNameElement.nextSiblingElement("LastName");
        QString lName = lNameElement.text();
        qDebug() << lName; // it outputs " "

        //Address
        QDomElement addressElement = lNameElement.nextSiblingElement("Address");
        QString address = addressElement.text();
        qDebug() << address; // it outputs " "

        //create owner object
        Owner newOwner;
        newOwner.setFirstName(fName);
        newOwner.setLastName(lName);
        newOwner.setAddress(address);
    }
}
4

1 回答 1

0

您的 XML 示例格式不正确:

<FirstName> John <FirstName/>

应该是:

<FirstName>John</FirstName>

在这里查看更多。

于 2013-09-18T21:35:44.767 回答