# HG changeset patch # User Markus Bröker # Date 1239878952 -7200 # Node ID 574503cf7bb09fc5c4898e400f9ee3c121b92bb7 # Parent be3f5582b839558a4a458bfd5584a350565861be xml parsing demo committer: Markus Bröker diff --git a/Makefile b/Makefile --- a/Makefile +++ b/Makefile @@ -45,7 +45,8 @@ sort \ min2time \ recursive_compiler \ - getpwnam_error + getpwnam_error \ + xmlparser .SUFFIXES: .c .cc .asm @@ -245,6 +246,10 @@ @echo Linking $< ... @$(CC) -o $@ $< +xmlparser: xmlparser.o + @echo Linking $< ... + @$(CC) -o $@ $< -lexpat + .PHONY: clean uninstall clean: diff --git a/xmlparser.c b/xmlparser.c new file mode 100644 --- /dev/null +++ b/xmlparser.c @@ -0,0 +1,66 @@ +/** + * test/demos/xmlparser.c + * Copyright (C) 2008 Markus Broeker + */ + +#include +#include +#include +#include + +#define MAXCHARS 80 + +void start (void *data, const XML_Char * name, const XML_Char ** atts) +{ + int i = 0; + printf ("START TAG: %s\n", name); + + while (atts[i] != NULL) { + printf ("\t[%.2d]: %s\n", i, atts[i]); + i++; + } +} + +void end (void *data, const XML_Char * name) +{ + printf (" END TAG: %s\n", name); +} + +int main (int argc, char **argv) +{ + XML_Parser parser; + FILE *f; + char buffer[MAXCHARS + 1]; + + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + if ((f = fopen (argv[1], "r")) == NULL) { + fprintf (stderr, "Cannot open %s\n", argv[1]); + perror ("FOPEN"); + return EXIT_FAILURE; + } + + if ((parser = XML_ParserCreate (NULL)) == NULL) { + perror ("XML_ParserCreate"); + return EXIT_FAILURE; + } + + XML_SetElementHandler (parser, &start, &end); + + while (fgets (buffer, MAXCHARS, f) != NULL) { + if (!XML_Parse (parser, buffer, strlen (buffer), 0)) + printf ("XML-ERROR: %s\n", XML_ErrorString (XML_GetErrorCode (parser))); + } + + XML_ParserFree (parser); + + if (fclose (f)) { + perror ("FCLOSE"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +}