xmlparser.c
author Emilio Largo <largo@largo.homelinux.org>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 45 7197576fedcf
parent 41 574503cf7bb0
child 77 49e0babccb23
permissions -rw-r--r--
pmc: namespace algebra for vector -> the namespace prevents clashes with std::vector -> std::vector and algebra::Vector with capital V. md5: a md5 replacement ncurses: keypad function added committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * test/demos/xmlparser.c
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <string.h>
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <expat.h>
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#define MAXCHARS 80
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
void start (void *data, const XML_Char * name, const XML_Char ** atts)
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
{
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
    int i = 0;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
    printf ("START TAG: %s\n", name);
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    while (atts[i] != NULL) {
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
        printf ("\t[%.2d]: %s\n", i, atts[i]);
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
        i++;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    }
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
}
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
void end (void *data, const XML_Char * name)
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
{
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    printf ("  END TAG: %s\n", name);
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
}
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
int main (int argc, char **argv)
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
{
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    XML_Parser parser;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    FILE *f;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    char buffer[MAXCHARS + 1];
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
    if (argc != 2) {
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
        printf ("Usage: %s <xmlfile>\n", argv[0]);
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
        return EXIT_FAILURE;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    }
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
    if ((f = fopen (argv[1], "r")) == NULL) {
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        fprintf (stderr, "Cannot open %s\n", argv[1]);
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
        perror ("FOPEN");
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
        return EXIT_FAILURE;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
    }
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    if ((parser = XML_ParserCreate (NULL)) == NULL) {
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
        perror ("XML_ParserCreate");
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        return EXIT_FAILURE;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    }
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
    XML_SetElementHandler (parser, &start, &end);
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
    while (fgets (buffer, MAXCHARS, f) != NULL) {
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
        if (!XML_Parse (parser, buffer, strlen (buffer), 0))
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
            printf ("XML-ERROR: %s\n", XML_ErrorString (XML_GetErrorCode (parser)));
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    }
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    XML_ParserFree (parser);
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
    if (fclose (f)) {
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        perror ("FCLOSE");
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
        return EXIT_FAILURE;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
    }
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    return EXIT_SUCCESS;
574503cf7bb0 xml parsing demo
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
}