关于XML标签中的键值该如何解析? #635
Answered
by
qicosmos
lamliukang
asked this question in
Q&A
-
|
Beta Was this translation helpful? Give feedback.
Answered by
qicosmos
Mar 18, 2024
Replies: 1 comment
-
struct book_t {
std::string title;
std::string author;
};
REFLECTION(book_t, title, author);
struct library {
iguana::xml_attr_t<book_t> book;
};
REFLECTION(library, book);
void lib_example() {
std::string str = R"(
<library name="UESTC library">
<book id="1234" language="en" edition="1">
<title>Harry Potter and the Philosopher's Stone</title>
<author>J.K. Rowling</author>
</book>
</library>
)";
{
iguana::xml_attr_t<library> lib;
iguana::from_xml(lib, str);
auto attr = lib.attr();
for (auto [k, v] : attr) {
std::cout << k << ": " << v << "\n";
}
} 将会打印出属性名字和值:
{
iguana::xml_attr_t<library> lib;
lib.attr().emplace("name", "tom");
iguana::xml_attr_t<book_t> book;
book.attr()["id"] = "42";
book.attr().emplace("language", "cn");
book.value().author = "tom";
book.value().title = "a story";
lib.value().book = book;
std::string str;
iguana::to_xml<true>(lib, str);
std::cout << str << "\n";
} 将会打印:
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
poor-circle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
将会打印出属性名字和值:
name: UESTC library