Home | History | Annotate | Download | only in tutorial
      1 <![CDATA[
      2 #include <stdio.h>
      3 #include <string.h>
      4 #include <stdlib.h>
      5 #include <libxml/xmlmemory.h>
      6 #include <libxml/parser.h>
      7 
      8 void
      9 parseStory (xmlDocPtr doc, xmlNodePtr cur) {
     10 
     11 	xmlChar *key;
     12 	cur = cur->xmlChildrenNode;
     13 	while (cur != NULL) {
     14 	    if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
     15 		    key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
     16 		    printf("keyword: %s\n", key);
     17 		    xmlFree(key);
     18  	    }
     19 	cur = cur->next;
     20 	}
     21     return;
     22 }
     23 
     24 static void
     25 parseDoc(char *docname) {
     26 
     27 	xmlDocPtr doc;
     28 	xmlNodePtr cur;
     29 
     30 	doc = xmlParseFile(docname);
     31 
     32 	if (doc == NULL ) {
     33 		fprintf(stderr,"Document not parsed successfully. \n");
     34 		return;
     35 	}
     36 
     37 	cur = xmlDocGetRootElement(doc);
     38 
     39 	if (cur == NULL) {
     40 		fprintf(stderr,"empty document\n");
     41 		xmlFreeDoc(doc);
     42 		return;
     43 	}
     44 
     45 	if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
     46 		fprintf(stderr,"document of the wrong type, root node != story");
     47 		xmlFreeDoc(doc);
     48 		return;
     49 	}
     50 
     51 	cur = cur->xmlChildrenNode;
     52 	while (cur != NULL) {
     53 		if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
     54 			parseStory (doc, cur);
     55 		}
     56 
     57 	cur = cur->next;
     58 	}
     59 
     60 	xmlFreeDoc(doc);
     61 	return;
     62 }
     63 
     64 int
     65 main(int argc, char **argv) {
     66 
     67 	char *docname;
     68 
     69 	if (argc <= 1) {
     70 		printf("Usage: %s docname\n", argv[0]);
     71 		return(0);
     72 	}
     73 
     74 	docname = argv[1];
     75 	parseDoc (docname);
     76 
     77 	return (1);
     78 }
     79 ]]>
     80