簡體   English   中英

如何在libxml2中獲取屬性並保存在c ++的stl映射中?

[英]How to get Attributes in libxml2 and save in stl map for c++?

我想使用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);
}

當然,在此示例中不考慮名稱空間。 如果使用名稱空間,則可以嘗試以下操作:

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

當然,您應該徹底檢查NULL值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM