libxml2解析非元素数据

时间:2013-01-15 17:41:17

标签: c xml-parsing libxml2

如果我的行如下所示:

msg = <temp id="0" name="Board" low="-1" high="90" > 45 </temp>

如何获取标签之间的值(即“45”)。这是我获取元素数据的代码:

doc = xmlReadMemory(msg, strlen(msg), "noname.xml", NULL, 0);
cur = xmlDocGetRootElement(doc);

void
print_element_names(xmlNode * a_node)
{
    xmlNode *cur_node   = NULL;
    char    *msgID      = NULL;
    char    *name       = NULL;
    char    *tmpL       = NULL;
    char    *tmpH       = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        msgID     = (char *)xmlGetProp(cur_node, (const xmlChar *)"id");
        name      = (char *)xmlGetProp(cur_node, (const xmlChar *)"name");
        tmpL      = (char *)xmlGetProp(cur_node, (const xmlChar *)"low");
        tmpH      = (char *)xmlGetProp(cur_node, (const xmlChar *)"high");

        if (cur_node->type == XML_ELEMENT_NODE) {
            if((msgID != NULL) && (name != NULL))
            printf("id: %s, name: %s, low: %s, high: %s, xml: %s\n", msgID, name, tmpL, tmpH);
        }

        print_element_names(cur_node->children);
    }
}

0 个答案:

没有答案