|
1 /** |
|
2 * test/demos/xmlparser.c |
|
3 * Copyright (C) 2008 Markus Broeker |
|
4 */ |
|
5 |
|
6 #include <stdio.h> |
|
7 #include <stdlib.h> |
|
8 #include <string.h> |
|
9 #include <expat.h> |
|
10 |
|
11 #define MAXCHARS 80 |
|
12 |
|
13 void start (void *data, const XML_Char * name, const XML_Char ** atts) |
|
14 { |
|
15 int i = 0; |
|
16 printf ("START TAG: %s\n", name); |
|
17 |
|
18 while (atts[i] != NULL) { |
|
19 printf ("\t[%.2d]: %s\n", i, atts[i]); |
|
20 i++; |
|
21 } |
|
22 } |
|
23 |
|
24 void end (void *data, const XML_Char * name) |
|
25 { |
|
26 printf (" END TAG: %s\n", name); |
|
27 } |
|
28 |
|
29 int main (int argc, char **argv) |
|
30 { |
|
31 XML_Parser parser; |
|
32 FILE *f; |
|
33 char buffer[MAXCHARS + 1]; |
|
34 |
|
35 if (argc != 2) { |
|
36 printf ("Usage: %s <xmlfile>\n", argv[0]); |
|
37 return EXIT_FAILURE; |
|
38 } |
|
39 |
|
40 if ((f = fopen (argv[1], "r")) == NULL) { |
|
41 fprintf (stderr, "Cannot open %s\n", argv[1]); |
|
42 perror ("FOPEN"); |
|
43 return EXIT_FAILURE; |
|
44 } |
|
45 |
|
46 if ((parser = XML_ParserCreate (NULL)) == NULL) { |
|
47 perror ("XML_ParserCreate"); |
|
48 return EXIT_FAILURE; |
|
49 } |
|
50 |
|
51 XML_SetElementHandler (parser, &start, &end); |
|
52 |
|
53 while (fgets (buffer, MAXCHARS, f) != NULL) { |
|
54 if (!XML_Parse (parser, buffer, strlen (buffer), 0)) |
|
55 printf ("XML-ERROR: %s\n", XML_ErrorString (XML_GetErrorCode (parser))); |
|
56 } |
|
57 |
|
58 XML_ParserFree (parser); |
|
59 |
|
60 if (fclose (f)) { |
|
61 perror ("FCLOSE"); |
|
62 return EXIT_FAILURE; |
|
63 } |
|
64 |
|
65 return EXIT_SUCCESS; |
|
66 } |