Reputation: 601
I have the xml file as:
<Config>
<tlvid id="2">
<type>10</type>
<Devid>001b00100000</Devid>
</tlvid>
<tlvid id="3">
<sessionid>abcd123</sessionid>
</tlvid>
The code which parses the xml file is:
xmlNode *cur_node = NULL,*sub_node = NULL;
xmlChar *key;
cur_node = a_node->xmlChildrenNode;
while(cur_node !=NULL) {
if((!xmlStrcmp(cur_node->name,(const xmlChar*)"tlvid"))) {
key = xmlGetProp(cur_node,(const xmlChar*)"id");
printf("key: %s\n ",key);
xmlFree(key);
sub_node = cur_node->xmlChildrenNode;
while(sub_node !=NULL) {
key = xmlNodeGetContent(sub_node);
printf("subkey: %s\n ",key);
xmlFree(key);
sub_node = sub_node->next;
}
}
cur_node = cur_node->next;
}
The output as:
key: 2
subkey:
subkey: 10
subkey:
subkey: 001b00100000
subkey:
key: 3
subkey:
subkey: abcd123
subkey:
I have tried xmlKeepBlanksDefault(0); adding under while loop to avoid blanks,but did not help. Can you please help me in removing these empty blanks. Thanks.
Upvotes: 0
Views: 4425
Reputation: 60988
Avoid processing of the text children of cur_node
by checking xmlNodeIsText
:
for(sub_node = cur_node->xmlChildrenNode;
sub_node != NULL;
sub_node = sub_node->next)
{
if(xmlNodeIsText(sub_node)) continue;
…
}
As an alternative to skipping all text nodes, you can make sure only to skip blank nodes by using xmlIsBlankNode
:
for(sub_node = cur_node->xmlChildrenNode;
sub_node != NULL;
sub_node = sub_node->next)
{
if(xmlIsBlankNode(sub_node)) continue;
…
}
The two results will differ if there is non-whitespace text directly within a <tlvid>
element.
Read the manual for xmlKeepBlanksDefault
to find out the conditions required to make the parser ignore these blank nodes. Apparently you need a validating parser and a suitable DTD for this to have any effect.
Upvotes: 2