简体   繁体   English

RapidXML 怪异 output

[英]RapidXML weird output

I'm currently trying to use RapidXML to write a file out, this is the code I've got for it我目前正在尝试使用 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();

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:它从 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"},
                                                         };

but when I look at the output file it just shows this:但是当我查看 output 文件时,它只显示了这个:

<?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: QStringToConstCharPoint function 定义如下:

const char* QStringToConstCharPoint(QString input) {
    QByteArray ba = input.toLocal8Bit();
    const char *result = ba.data();
    return result;
}

Why is my output looking like that?为什么我的 output 是这样的? Have I done something wrong?我做错了什么吗?

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.原来 QStringToConstCharPoint function 是问题所在,它与 RapidXML 搞砸了,我不知道为什么,但我找到了更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM