tinyxml2 сборка и запись файла xml на c ++ - PullRequest
3 голосов
/ 29 февраля 2020

Я пытаюсь создать простой файл xml с использованием tinyxml2 в VS2019, по какой-то причине код работает, пока не достигнет элемента с именем port. этот элемент и каждый элемент после него игнорируется в файле xml. Я только что заметил, что он также пишет xml вывод в неправильном порядке, хост должен быть справа внизу, а не порт.

Что мне здесь не хватает?

По правде говоря, у меня есть около 2 дней опыта написания с ++, и очень основы c python knowlage.

#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>

1 Ответ

2 голосов
/ 29 февраля 2020
XMLElement* user = xmlDoc.NewElement("user");
user->SetText("test");
subchild->InsertEndChild(host);

должно быть

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