gpt4 book ai didi

c++ - QT用DOM读取XML文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:02:05 25 4
gpt4 key购买 nike

我想读取 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);
}
}

最佳答案

您的 XML 样本格式不正确:

<FirstName> John <FirstName/>

应该改为:

<FirstName>John</FirstName>

See more here.

关于c++ - QT用DOM读取XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18874664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com