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, char *keyword) {
     10 
     11 	xmlNewTextChild (cur, NULL, "keyword", keyword);
     12     return;
     13 }
     14 
     15 xmlDocPtr
     16 parseDoc(char *docname, char *keyword) {
     17 
     18 	xmlDocPtr doc;
     19 	xmlNodePtr cur;
     20 
     21 	doc = xmlParseFile(docname);
     22 
     23 	if (doc == NULL ) {
     24 		fprintf(stderr,"Document not parsed successfully. \n");
     25 		return (NULL);
     26 	}
     27 
     28 	cur = xmlDocGetRootElement(doc);
     29 
     30 	if (cur == NULL) {
     31 		fprintf(stderr,"empty document\n");
     32 		xmlFreeDoc(doc);
     33 		return (NULL);
     34 	}
     35 
     36 	if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
     37 		fprintf(stderr,"document of the wrong type, root node != story");
     38 		xmlFreeDoc(doc);
     39 		return (NULL);
     40 	}
     41 
     42 	cur = cur->xmlChildrenNode;
     43 	while (cur != NULL) {
     44 		if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
     45 			parseStory (doc, cur, keyword);
     46 		}
     47 
     48 	cur = cur->next;
     49 	}
     50 	return(doc);
     51 }
     52 
     53 int
     54 main(int argc, char **argv) {
     55 
     56 	char *docname;
     57 	char *keyword;
     58 	xmlDocPtr doc;
     59 
     60 	if (argc <= 2) {
     61 		printf("Usage: %s docname, keyword\n", argv[0]);
     62 		return(0);
     63 	}
     64 
     65 	docname = argv[1];
     66 	keyword = argv[2];
     67 	doc = parseDoc (docname, keyword);
     68 	if (doc != NULL) {
     69 		xmlSaveFormatFile (docname, doc, 0);
     70 		xmlFreeDoc(doc);
     71 	}
     72 
     73 	return (1);
     74 }
     75 ]]>
     76