gpt4 book ai didi

c++ - 是否可以使用 TinyXML2(或任何其他库)将结构或类直接转换为 XML 文件,反之亦然?

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:23 44 4
gpt4 key购买 nike

我有很多这样的结构:

struct A
{
long lValueA;
BOOL bValueA;
CString strValueA;
};
struct B
{
long lValueB;
BOOL bValueB;
CString strValueB;
};
struct C
{
A a;
vector<B> vecB;
};

是否可以使用 TinyXML2(或任何其他库)将其转换为 XML 文件,而无需手动传递来自 struct C 的每个成员变量?我想要的是这样的:

main()
{
C c;
// Some code to initialise member variable of struct C

// pass object/structure to XML parser to get XML file.
Some_XML_Library_Object.parse( c );
Some_XML_Library_Object.SaveFile("FilePath/Name.xml");

// Also it would be nice if we can update values in structure or class directly like this
const char* XML_File_Path = "FilePath/Name.xml";
Some_XML_Library_Object.updateValueOfStructureFromXML(&c,XML_File_Path)
}

生成的 XML 文件类似于:

<?xml version="1.0" encoding="UTF-8"?>
<A>
<lValueA>
value
</lValueA>

<bValueA>
value
</bValueA>

<strValueA>
value
</strValueA>
</A>

<B>
<lValueB>
value
</lValueB>
...
...
</B>

提前致谢。

最佳答案

你可以使用 boost::serialisation 来做你要求的事情......

http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/tutorial.html

它具有处理上述页面中的 XML(和其他格式)的广泛功能:

In this tutorial, we have used a particular archive class - text_oarchive for saving and text_iarchive for loading. text archives render data as text and are portable across platforms. In addition to text archives, the library includes archive class for native binary data and xml formatted data. Interfaces to all archive classes are all identical. Once serialization has been defined for a class, that class can be serialized to any type of archive.

编辑:您将处理对象并将它们序列化,而不是直接操作 XML 来更改值。

关于c++ - 是否可以使用 TinyXML2(或任何其他库)将结构或类直接转换为 XML 文件,反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15632479/

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