0

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.

4

1 回答 1

2

cur_node通过检查避免处理文本子项xmlNodeIsText

for(sub_node = cur_node->xmlChildrenNode;
    sub_node != NULL;
    sub_node = sub_node->next)
{
    if(xmlNodeIsText(sub_node)) continue;
    …
}

作为跳过所有文本节点的替代方法,您可以使用以下方法确保仅跳过空白节点xmlIsBlankNode

for(sub_node = cur_node->xmlChildrenNode;
    sub_node != NULL;
    sub_node = sub_node->next)
{
    if(xmlIsBlankNode(sub_node)) continue;
    …
}

<tlvid>如果元素中直接存在非空白文本,则这两个结果会有所不同。

阅读手册xmlKeepBlanksDefault以找出使解析器忽略这些空白节点所需的条件。显然,您需要一个验证解析器和一个合适的 DTD 才能产生任何效果。

于 2012-09-05T11:44:41.930 回答