I'd like to hang some private data (void*) off of nodes in a DOM tree using the libxml2 or gdome APIs. Looking through the (rather sparse) api docs, it seems like the libxml2 nodes might have a 开发者_开发知识库member for user data (_private, on almost every object), but I didn't see any api calls for manipulating this.
As the xmlNode is a struct you can directly access the void* _private
members.
xmlNode * ePtr = ...;
ePtr->_private = ...;
But I can't find any documentation saying that field is for custom user data. So I'd avoid that approach.
Instead I'd use a separate metadata store, something like std::map<xmlNode*, void*>
would do. (I'd use something a little more type-safe than void*
but hopefully you get the idea)
The biggest problem that I see (in both cases) is that you need to be careful about the lifetime of the metadata you associate. I can see no way to be notified of the destruction of a node ... which may lead to all kinds of trouble.
The documentation says For user data, libxml won't touch it about _private
, but my recollection is that it is meant for things like libxml2 language bindings (e.g. xmlwrapp uses it for that purpose), not for user code.
精彩评论