how to get the keys of a xmlHashTable ? (libxml2)

我的梦境 提交于 2019-12-07 16:15:31

There is the xmlHashScan() function (and its pickier relatives), which can be used to have a user-provided xmlHashScanner function called back by the library for each entry in the xmlHashTable. The xmlHashScanner is given the name of each entry as parameter, so it should be easy to assemble a list of all the keys that way.

nwellnhof

Your question is a typical XY problem. An easier way to get the entity declarations is to iterate the children of the DTD node:

xmlDtdPtr dtd = doc->intSubset;

for (xmlNodePtr child = dtd->children; child; child = child->next) {
    if (child->type == XML_ENTITY_DECL) {
        printf("%s\n", child->name);
    }
}

For those who want to know more details (especially the code), you can look at my following code which helps me get the key list:

typedef struct _entities_key_list{
    xmlChar *name;
    struct _entities_key_list *next;
} entities_key_list;

static entities_key_list *head_key_list = NULL;
static entities_key_list *current_key_list = NULL;

/**
 * @brief Callback when scanning data in a hash with the simple scanner
 * @param the data in the hash
 * @param extra scanner data
 * @param the name associated
 * @return void
 */
void the_callback_function(void *payload, void *data, xmlChar *name){
    if(!head_key_list)
    {
        head_key_list = current_key_list = g_new(entities_key_list, 1);
        head_key_list->name = name;
        head_key_list->next = NULL;
    }
    else
    {
        current_key_list = current_key_list->next = g_new(entities_key_list, 1);
        current_key_list->name = name;
        current_key_list->next = NULL;
    }
}

/**
 * @brief Parses the entity inforamtion in the XML document: dictionary.xml
 * @param ptr pointer to an XML document
 * @return pointer to a linked list of type entity_t
 */
entity_t *parse_entities(xmlDocPtr ptr) 
{
    xmlHashScanner ptr27 = &the_callback_function;
    xmlHashTablePtr ptr28 = (xmlHashTablePtr) ptr->intSubset->entities;
    xmlHashScan(ptr28, ptr27, NULL);
    puts("**************************************************");
    puts("let's test if I have got the great result!");
    printf("the result is: %s\n", head_key_list->name);
    printf("the result is: %s\n", head_key_list->next->name);
    puts("**************************************************");
... //code that is not interesting anymore
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!