libxml C解析xml文档

1. 代码

#include 
#include 
#include 

void parseDoc(char *filename);
void printChildrenNames(xmlDocPtr doc, xmlNodePtr cur);

//解析xml文件
void parseDoc(char *filename) {

    xmlDocPtr doc;
    xmlNodePtr cur;
    
    xmlKeepBlanksDefault(0);
	//解析文档
    doc = xmlParseFile(filename);
    
    if (doc == NULL ) {
        fprintf(stderr,"Document not parsed successfully.\n");
        return;
    }

    //获取根结点
    cur = xmlDocGetRootElement(doc);
	//输出根结点信息
	printf("root node=%s\n", cur->name);
    
    if (cur == NULL) {
        fprintf(stderr,"empty document\n");
        xmlFreeDoc(doc);
        return;
    }
    
	//打印子结点信息
	printChildrenNames(doc, cur);

	xmlFreeDoc(doc);
    return;
}

//打印子结点信息
void printChildrenNames(xmlDocPtr doc, xmlNodePtr cur) {
	xmlChar *key;
	xmlChar* attr_value;
    if (cur != NULL) {
        cur = cur->xmlChildrenNode;
        
        while (cur != NULL){
			//如果是元素结点,则输出其名称和内容
			if(cur->type == XML_ELEMENT_NODE){
            	printf("Current Node: %s\t\t", cur->name);
				//获取结点内容
				key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
				if(key != NULL)
					printf("content: %s\t\t", key);
				xmlFree(key);
				//获取结点属性,data为属性名
				attr_value = xmlGetProp(cur, (const xmlChar *)"data");
				if(attr_value != NULL)
					printf("data: %s",attr_value);
				xmlFree(attr_value);
				printf("\n");
			}
            printChildrenNames(doc, cur);
            cur = cur->next;
        }
        
        return;
    }else{
        fprintf(stderr, "ERROR: Null Node!");
        return;
    }
}

int main(int argc, char **argv) {
    if (argc != 2) {
		printf("Parameter too litter!\n");
        return(1);
	}

    /*
     * 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

	parseDoc(argv[1]);

    /*
     * Cleanup function for the XML library
     */
    xmlCleanupParser();
    /*
     * this is to debug memory for regression tests
     */
    xmlMemoryDump();
    return(0);
}

2. 编译

使用gcc

gcc -o example `xml2-config --cflags` example.c `xml2-config --libs`

或Makefile

EXES = $(basename $(wildcard *.c))
CC = cc
CFLAGS = -Wall -g `xml2-config --cflags` `xml2-config --libs`

all: 
	$(MAKE) $(EXES)

%: %.c
	$(CC) $(CFLAGS) [email protected] -o $@

clean:
	rm -f $(EXES) *.o
此Makefile会以同样的方式,编译本目录下所有.c源文件。


3. 解析文档,输出结果

root@ubuntu:~/下载/xml# ./parse_me d.xml
root node=note
Current Node: to		content: George		
Current Node: from		content: John		
Current Node: heading		content: Reminder		
Current Node: body		content: Don't forget the meeting!		
root@ubuntu:~/下载/xml# ./parse_me wea2.xml
root node=xml_api_reply
Current Node: weather		
Current Node: forecast_information		
Current Node: city		data: 
Current Node: postal_code		data: 
Current Node: latitude_e6		data: 30670000
Current Node: longitude_e6		data: 104019996
Current Node: forecast_date		data: 2012-04-06
Current Node: current_date_time		data: 2012-04-07 02:00:00 +0000
Current Node: unit_system		data: SI
Current Node: current_conditions		
Current Node: condition		data: 多云
Current Node: temp_f		data: 63
Current Node: temp_c		data: 17
Current Node: humidity		data: 湿度: 68%
Current Node: icon		data: /ig/images/weather/cn_cloudy.gif
Current Node: wind_condition		data: 风向: 南、风速:1 米/秒
Current Node: forecast_conditions		
Current Node: day_of_week		data: 周五
Current Node: low		data: 15
Current Node: high		data: 25
Current Node: icon		data: /ig/images/weather/mostly_sunny.gif
Current Node: condition		data: 以晴为主
Current Node: forecast_conditions		
Current Node: day_of_week		data: 周六
Current Node: low		data: 13
Current Node: high		data: 23
Current Node: icon		data: /ig/images/weather/mostly_sunny.gif
Current Node: condition		data: 晴间多云
Current Node: forecast_conditions		
Current Node: day_of_week		data: 周日
Current Node: low		data: 13
Current Node: high		data: 22
Current Node: icon		data: /ig/images/weather/cn_fog.gif
Current Node: condition		data: 雾
Current Node: forecast_conditions		
Current Node: day_of_week		data: 周一
Current Node: low		data: 12
Current Node: high		data: 24
Current Node: icon		data: /ig/images/weather/cn_fog.gif
Current Node: condition		data: 雾






你可能感兴趣的:(Linux,C/C++)