gpt4 book ai didi

c++ - 修改内容并将其添加到XML文件

转载 作者:行者123 更新时间:2023-12-02 10:20:55 25 4
gpt4 key购买 nike

我有以下格式的XML文件。

<!DOCTYPE newproject>
<subject_details>
<Name>Name5</Name>
<Surname>Surname5</Surname>
<Patient_ID>PID05</Patient_ID>
<Date>01/01/2000 00:00</Date>
<Clinician_Note>note note</Clinician_Note>
<Settings>
<Current>19</Current>
<PW>25</PW>
<Freq>26</Freq>
</Settings>
</subject_details>

其中 Settings节点的子代为 CurrentPWFreq。三种不同 QSlider的值。

为了将内容添加到 Settings节点,我执行以下步骤,如下所示。
  • 以只读方式打开XML文件,全部读取并关闭它。
  • 在内存文档中进行更改。
  • 然后打开文件进行覆盖,写入所有内容,关闭文件。

  • 看到代码:
         //Open the file read-only, read it all in, close it.
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    qDebug () << "Error saving XML file....";
    QMessageBox::information(this, "Unable to open file for read", file.errorString());
    return;
    }

    QDomDocument document;
    document.setContent(&file);
    QDomElement root = document.documentElement();

    file.close();

    // Make changes in-memory document.

    QDomElement newTag = document.createElement(QString("Settings"));

    QDomNode SettingsNode = root.elementsByTagName("Settings").at(0).firstChild();
    QDomElement SettingsNodeVal = SettingsNode.toElement();

    if (SettingsNodeVal.isNull())
    {

    QDomElement newCurrTag = document.createElement(QString("Current"));
    QDomText newCurrVal = document.createTextNode(currVal);
    newCurrTag.appendChild(newCurrVal);
    newTag.appendChild(newCurrTag);

    QDomElement newPWTag = document.createElement(QString("PW"));
    QDomText newPWVal = document.createTextNode(PWVal);
    newPWTag.appendChild(newPWVal);
    newTag.appendChild(newPWTag);

    QDomElement newFreqTag = document.createElement(QString("Freq"));
    QDomText newFreqVal = document.createTextNode(FreqVal);
    newFreqTag.appendChild(newFreqVal);
    newTag.appendChild(newFreqTag);


    root.appendChild(newTag);
    }

    //Then open the file for overwrite, write all content, close file.

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
    qDebug () << "Error saving XML file....";
    QMessageBox::information(this, "Unable to open file for write", file.errorString());
    return;
    }

    QTextStream output(&file);
    output << document.toString();
    file.close();

    }

    如果此处为空,则可以在此处将Children添加到 Settings节点。这很好。

    我的下一步:

    我想修改现有的 Settings节点子级( CurrentPWFreq等)。

    有人可以告诉我在上面的示例中如何修改现有的节点值吗?

    提前致谢

    最佳答案

    我们可以使用setNodeValue修改文本节点中的值,例如:

          QDomElement root = document.documentElement();
    QDomNode SettingsNode = root.namedItem("Settings");

    QDomNode cur = SettingsNode.namedItem("Current");
    cur.firstChild().setNodeValue("new value1"); // Note the extra FirstChild() its to get the INNER QDomText

    QDomNode pw = SettingsNode.namedItem("PW");
    pw.firstChild().setNodeValue("new value2");

    QDomNode freq = SettingsNode.namedItem("Freq");
    freq.firstChild().setNodeValue("new value3");

    关于c++ - 修改内容并将其添加到XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60228030/

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