我使用库libxml2来解析XML文档。然后我在这个库中看到了xmlHashTable
。是否可以获得xmlHashTable
的所有密钥?我想将所有键存储在一个数组中。
XML文档是:
<?xml version="1.0" encoding="UTF-8"?>
<?type-proto key="MIPRegistrationRequest" value="mip" ?>
<?avp-proto key="Example-AVP" value="data" ?>
<!DOCTYPE dictionary SYSTEM "dictionary.dtd" [
<!-- Any files added here need to be added to Makefile.am and
packaging/nsis/wireshark.nsi -->
<!ENTITY nasreq SYSTEM "nasreq.xml">
<!ENTITY eap SYSTEM "eap.xml">
<!ENTITY mobileipv4 SYSTEM "mobileipv4.xml">
<!ENTITY chargecontrol SYSTEM "chargecontrol.xml">
<!ENTITY sunping SYSTEM "sunping.xml">
<!ENTITY TGPP SYSTEM "TGPP.xml">
<!ENTITY TGPP2 SYSTEM "TGPP2.xml">
<!ENTITY sip SYSTEM "sip.xml">
<!ENTITY etsie2e4 SYSTEM "etsie2e4.xml">
<!ENTITY Ericsson SYSTEM "Ericsson.xml">
<!ENTITY mobileipv6 SYSTEM "mobileipv6.xml">
<!ENTITY Cisco SYSTEM "Cisco.xml">
<!ENTITY Starent SYSTEM "Starent.xml">
<!ENTITY Vodafone SYSTEM "Vodafone.xml">
<!ENTITY AlcatelLucent SYSTEM "AlcatelLucent.xml">
<!ENTITY Nokia SYSTEM "Nokia.xml">
<!ENTITY NokiaSolutionsAndNetworks SYSTEM "NokiaSolutionsAndNetworks.xml">
<!ENTITY HP SYSTEM "HP.xml">
<!ENTITY Oracle SYSTEM "Oracle.xml">
<!ENTITY Custom SYSTEM "Custom.xml">
]>
<dictionary>
...
<dictionary>
我试图获取外部(解析)实体的所有键(例如nasreq,eap等):
ddict_t *ddict_scan(const char* system_directory, const char* filename)
{
char *path = g_strconcat((const gchar*) system_directory, (const gchar*)filename, (const gchar *)'\0');
xmlDocPtr p1 = getdoc((const char *)path);
xmlEntitiesTablePtr ptr2 = p1->intSubset->entities;
//to be added
}
xmlEntitiesTable
的类型是xmlHashTable
。
答案 0 :(得分:1)
有xmlHashScan()函数(及其pickier亲属),可用于为xmlHashTable
中的每个条目库提供一个用户提供的xmlHashScanner函数。 。 xmlHashScanner
被赋予每个条目的name
作为参数,因此应该很容易汇总所有密钥的列表。
答案 1 :(得分:1)
您的问题是典型的XY problem。获取实体声明的更简单方法是迭代DTD节点的子节点:
xmlDtdPtr dtd = doc->intSubset;
for (xmlNodePtr child = dtd->children; child; child = child->next) {
if (child->type == XML_ENTITY_DECL) {
printf("%s\n", child->name);
}
}
答案 2 :(得分:0)
对于那些想要了解更多详细信息(特别是代码)的人,您可以查看以下代码,以帮助我获取密钥列表:
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
}