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);
        }
    }
}

你可能感兴趣的:(xml,libxml2)