libxml2 读取xml节点的属性

xml读取基本5个步骤:

	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;
	
	/*
	* this initialize the library and check potential ABI mismatches
	* between the version it was compiled for and the actual shared
	* library used.
	*/
	LIBXML_TEST_VERSION
		
	/*parse the file and get the DOM */
	doc = xmlReadFile(credfile, NULL, 0);
	
	if (doc == NULL) {
		printf("error: could not parse file %s\n", credfile);
	}
	
	/*Get the root element node */
	root_element = xmlDocGetRootElement(doc);
	
	EnumXmlElement(root_element);

	/*free the document */
	xmlFreeDoc(doc);


节点枚举函数:

void EnumXmlElement(xmlNode * a_node)
{
    xmlNode *cur_node = NULL;
	// 遍历节点
	for (cur_node = a_node; cur_node; cur_node = cur_node->next) 
	{
        if (cur_node->type == XML_ELEMENT_NODE) 
		{
			// 打印节点名称
			printf("<%s>\n"	, cur_node->name);

			// 遍历属性列表
			xmlAttr * attr = cur_node->properties;
			while(attr)
			{				
				printf("\t%s=%s\n",attr->name,attr->children->content);
				attr = attr->next;
			}
			// 递归遍历子节点列表
			EnumXmlElement(cur_node->children);
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值