RapidXML 奇怪的输出
RapidXML weird output
我目前正在尝试使用 RapidXML 写出文件,这是我为它准备的代码
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();
它从 Qt(QLabel 或 QLineEdit)中读取一个值,然后将其与此处定义的成对向量(const QString、const QString)进行比较:
std::vector<std::pair<const QString,const QString>> params = {{"PopulationInput","population"},
{"XInput","AreaX"},
{"YInput","AreaY"},
{"AlreadyInfectedInput","AlreadyInfected"},
{"InfectionProbabilityIndicator","infectProbability"},
{"RadOfInfectionInput","infectionRadius"},
{"CPUThreadsDisplay","threads"},
};
但是当我查看输出文件时,它只显示:
<?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>
QStringToConstCharPoint 函数定义如下:
const char* QStringToConstCharPoint(QString input) {
QByteArray ba = input.toLocal8Bit();
const char *result = ba.data();
return result;
}
为什么我的输出看起来像那样?我是不是做错了什么?
原来问题出在 QStringToConstCharPoint 函数上,它与 RapidXML 搞砸了,我不知道为什么,但我找到了更好的方法。
我目前正在尝试使用 RapidXML 写出文件,这是我为它准备的代码
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();
它从 Qt(QLabel 或 QLineEdit)中读取一个值,然后将其与此处定义的成对向量(const QString、const QString)进行比较:
std::vector<std::pair<const QString,const QString>> params = {{"PopulationInput","population"},
{"XInput","AreaX"},
{"YInput","AreaY"},
{"AlreadyInfectedInput","AlreadyInfected"},
{"InfectionProbabilityIndicator","infectProbability"},
{"RadOfInfectionInput","infectionRadius"},
{"CPUThreadsDisplay","threads"},
};
但是当我查看输出文件时,它只显示:
<?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>
QStringToConstCharPoint 函数定义如下:
const char* QStringToConstCharPoint(QString input) {
QByteArray ba = input.toLocal8Bit();
const char *result = ba.data();
return result;
}
为什么我的输出看起来像那样?我是不是做错了什么?
原来问题出在 QStringToConstCharPoint 函数上,它与 RapidXML 搞砸了,我不知道为什么,但我找到了更好的方法。