3

我正在尝试在 VS2019 中使用 tinyxml2 构建一个简单的 xml 文件,由于某种原因,代码一直有效,直到它遇到名为 port 的元素。此元素及其后的每个元素在 xml 文件中都将被忽略。我刚刚注意到,它也在以错误的顺序写入 xml 输出,主机应该是下面的那个,而不是端口。

我在这里想念什么?

说实话,我有大约 2 天的 C++ 编写经验,以及非常基本的 Python 知识。

#include <iostream>
#include <fstream>
#include <windows.h>
#include <Lmcons.h>
#include <stdlib.h>
#include <filesystem>
#include "tinyxml/tinyxml2.h"

using namespace tinyxml2;
using namespace std;
namespace fs = std::filesystem;

int main()
{
    SetConsoleOutputCP(CP_UTF8);

    char* appdata;
    size_t len;
    errno_t err = _dupenv_s(&appdata, &len, "APPDATA");

    fs::path path = appdata;
    path /= "FileZilla";
    path /= "sitemanager.xml";

    TCHAR username[UNLEN + 1];
    DWORD size = UNLEN + 1;
    GetUserName((TCHAR*)username, &size);

    tinyxml2::XMLDocument xmlDoc;

    //tinyxml2::XMLDeclaration* decl = new XMLDeclaration("1.0", "UTF-8", "");


    XMLElement* pRoot = xmlDoc.NewElement("FileZilla3");
    pRoot->SetAttribute("Version", "");
    pRoot->SetAttribute("Platform", "");
    xmlDoc.InsertFirstChild(pRoot);

    XMLElement* child = xmlDoc.NewElement("Servers");
    child->SetText("\n");
    pRoot->InsertEndChild(child);

    XMLElement* subchild = xmlDoc.NewElement("Server");
    subchild->SetText("\n");
    child->InsertEndChild(subchild);

    XMLElement* host = xmlDoc.NewElement("host");
    host->SetText("ftp.some.url");
    subchild->InsertEndChild(host);

    XMLElement* port = xmlDoc.NewElement("port");
    port->SetText(21);
    subchild->InsertEndChild(port);

    XMLElement* protocol = xmlDoc.NewElement("Protocol");
    protocol->SetText(0);
    subchild->InsertEndChild(protocol);

    XMLElement* type = xmlDoc.NewElement("type");
    type->SetText(0);
    subchild->InsertEndChild(type);

    XMLElement* user = xmlDoc.NewElement("user");
    user->SetText("test");
    subchild->InsertEndChild(host);


    xmlDoc.SaveFile("SavedData.xml");

        cout << path << endl;
        std::wcout << username << endl;

    return 0;
}

输出文件如下所示:

<FileZilla3 Version="" platform="">
    <Servers>
        <Server>
            <port>21</port>
            <Protocol>0</Protocol>
            <type>0</type>
            <host>ftp.some.url</host>
        </Server>
    </Servers>
</FileZilla3>

所需的输出应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<FileZilla3 version="" platform="">
    <Servers>
        <Server>
            <Host>saddf</Host>
            <Port>21</Port>
            <Protocol>0</Protocol>
            <Type>0</Type>
            <User>username</User>
            <Pass encoding="base64" />
            <Logontype>1</Logontype>
            <PasvMode>MODE_DEFAULT</PasvMode>
            <EncodingType>Auto</EncodingType>
            <BypassProxy>0</BypassProxy>
            <Name>Ny tjener</Name>
            <SyncBrowsing>0</SyncBrowsing>
            <DirectoryComparison>0</DirectoryComparison>
        </Server>
    </Servers>
</FileZilla3>
4

1 回答 1

2
XMLElement* user = xmlDoc.NewElement("user");
user->SetText("test");
subchild->InsertEndChild(host);

应该

XMLElement* user = xmlDoc.NewElement("user");
user->SetText("test");
subchild->InsertEndChild(user);
于 2020-02-29T11:53:16.370 回答