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