xmlparser.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 58 9609c54a54d3
parent 41 574503cf7bb0
child 77 49e0babccb23
permissions -rw-r--r--
myprintf.c added - replaces varargs.c committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/xmlparser.c
 * Copyright (C) 2008 Markus Broeker
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <expat.h>

#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 <xmlfile>\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;
}