gpt4 book ai didi

c++ - 如何在 libxml2 中获取属性并保存在 C++ 的 STL 映射中?

转载 作者:行者123 更新时间:2023-11-28 08:15:23 25 4
gpt4 key购买 nike

我想使用 libxml2 获取 xml 的属性并将其保存到 C++ 中的 STL 映射?

最佳答案

如果你已经解析了你的 XML 字符串/文件,并且已经有一个你想要映射属性的节点,它应该是这样的:

xmlNodePtr yournode = ...;
std::map<std::string, std::string> yourmap;

for(xmlAttrPtr attr = yournode->properties; attr != NULL; attr = attr->next)
{
yourmap[attr->name] = xmlGetProp(yournode, attr->name);
}

当然,这个例子中不考虑 namespace 。如果您使用的是命名空间,则可以尝试以下操作:

yourmap[attr->name] = xmlGetNsProp(yournode, attr->name, attr->ns->href);

当然,您应该彻底检查 NULL 值。

关于c++ - 如何在 libxml2 中获取属性并保存在 C++ 的 STL 映射中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7872413/

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