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