Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tinyxml2 building and writing xml file on c++

Tags:

c++

tinyxml2

I am trying to build a simple xml file using tinyxml2 in VS2019, for some reason the code works until it hits the element named port. this element and every element after it is ignored in the xml file. I just noticed, it is also writing xml output in the wrong order, host should be the one right below , not port.

What am i missing here?

Truth be told, i have about 2 days experience writing c++, and very basic 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;
}

The output file looks like this:

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

the desired uotput should be this:

<?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>
like image 258
Anders Michael Friis Avatar asked Jan 20 '26 05:01

Anders Michael Friis


1 Answers

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

should be

XMLElement* user = xmlDoc.NewElement("user");
user->SetText("test");
subchild->InsertEndChild(user);
like image 136
Anders Michael Friis Avatar answered Jan 23 '26 19:01

Anders Michael Friis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!