gpt4 book ai didi

c++ - tinyxml2 在 C++ 上构建和写入 xml 文件

转载 作者:行者123 更新时间:2023-12-02 00:36:18 25 4
gpt4 key购买 nike

我正在尝试在 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>

最佳答案

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

应该是

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

关于c++ - tinyxml2 在 C++ 上构建和写入 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60464678/

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