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