Reputation: 26
I'm currently trying to use RapidXML to write a file out, this is the code I've got for it
xml_document<> doc;
xml_node<>* decl = doc.allocate_node(node_declaration);
decl->append_attribute(doc.allocate_attribute("version", "1.0"));
decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(decl);
xml_node<> *root = doc.allocate_node(node_element, "config");
for(int i = 0; i < params.size(); i++) {
bool LineEdit = false;
std::pair<QLabel*, QLineEdit*> LabelAndLine;
LabelAndLine.first = ui->centralwidget->findChild<QLabel*>(params.at(i).first);
if (LabelAndLine.first == nullptr) {
LineEdit = true;
LabelAndLine.second = ui->centralwidget->findChild<QLineEdit*>(params.at(i).first);
}
QString nodename = params.at(i).second;
qInfo() << "Setting: "+nodename+" from "+params.at(i).first;
xml_node<> *param = doc.allocate_node(node_element, QStringToConstCharPoint(nodename));
xml_attribute<char> *value;
if (LineEdit) {
value = doc.allocate_attribute("value", QStringToConstCharPoint(LabelAndLine.second->text()));
}
else {
value = doc.allocate_attribute("value", QStringToConstCharPoint(LabelAndLine.first->text()));
}
param->append_attribute(value);
root->append_node(param);
}
doc.append_node(root);
std::string xmlName = std::to_string(time(NULL))+".xml";
std::ofstream fileStored(xmlName);
fileStored << doc;
fileStored.close();
doc.clear();
It reads a value from Qt (either a QLabel or a QLineEdit) then compares it to a vector of pairs (const QString, const QString) defined here:
std::vector<std::pair<const QString,const QString>> params = {{"PopulationInput","population"},
{"XInput","AreaX"},
{"YInput","AreaY"},
{"AlreadyInfectedInput","AlreadyInfected"},
{"InfectionProbabilityIndicator","infectProbability"},
{"RadOfInfectionInput","infectionRadius"},
{"CPUThreadsDisplay","threads"},
};
but when I look at the output file it just shows this:
<?xml version="1.0" encoding="utf-8"?>
<config>
<ding="utf-8"?>
<config>
<ding="utf-8"?>
<co value='ding="utf-8"'/>
<ding="utf-8" value='ding="utf-8"'/>
<ding="utf-8" value='ding="utf-8"'/>
<ding="utf-8"?>
<config>
<ding="utf-8"?>
<co value='ding="utf-8"'/>
<ding="utf-8"?>
<config>
<ding="utf-8"?>
<co value='ding="utf-8"'/>
<ding="utf-8"?>
<config>
<ding="utf-8"?>
<co value='ding="utf-8"'/>
<ding="utf-8"?>
<config>
<ding="utf-8"?>
<co value='ding="utf-8"'/>
</config>
The QStringToConstCharPoint function is defined as this:
const char* QStringToConstCharPoint(QString input) {
QByteArray ba = input.toLocal8Bit();
const char *result = ba.data();
return result;
}
Why is my output looking like that? Have I done something wrong?
Upvotes: 0
Views: 132
Reputation: 26
Turns out the QStringToConstCharPoint function was the issue, it was messing something up with RapidXML and I don't know why, but I found a better way to do it.
Upvotes: 0