~ubuntu-branches/ubuntu/lucid/expat/lucid

« back to all changes in this revision

Viewing changes to examples/elements.c

  • Committer: Bazaar Package Importer
  • Author(s): Ardo van Rangelrooij
  • Date: 2001-12-09 12:19:40 UTC
  • Revision ID: james.westby@ubuntu.com-20011209121940-zu64acbvutfed4hh
Tags: upstream-1.95.2
ImportĀ upstreamĀ versionĀ 1.95.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This is simple demonstration of how to use expat. This program
 
2
reads an XML document from standard input and writes a line with the
 
3
name of each element to standard output indenting child elements by
 
4
one tab stop more than their parent element. */
 
5
 
 
6
#include <stdio.h>
 
7
#include "expat.h"
 
8
 
 
9
static void
 
10
startElement(void *userData, const char *name, const char **atts)
 
11
{
 
12
  int i;
 
13
  int *depthPtr = userData;
 
14
  for (i = 0; i < *depthPtr; i++)
 
15
    putchar('\t');
 
16
  puts(name);
 
17
  *depthPtr += 1;
 
18
}
 
19
 
 
20
static void
 
21
endElement(void *userData, const char *name)
 
22
{
 
23
  int *depthPtr = userData;
 
24
  *depthPtr -= 1;
 
25
}
 
26
 
 
27
int
 
28
main(int argc, char *argv[])
 
29
{
 
30
  char buf[BUFSIZ];
 
31
  XML_Parser parser = XML_ParserCreate(NULL);
 
32
  int done;
 
33
  int depth = 0;
 
34
  XML_SetUserData(parser, &depth);
 
35
  XML_SetElementHandler(parser, startElement, endElement);
 
36
  do {
 
37
    size_t len = fread(buf, 1, sizeof(buf), stdin);
 
38
    done = len < sizeof(buf);
 
39
    if (!XML_Parse(parser, buf, len, done)) {
 
40
      fprintf(stderr,
 
41
              "%s at line %d\n",
 
42
              XML_ErrorString(XML_GetErrorCode(parser)),
 
43
              XML_GetCurrentLineNumber(parser));
 
44
      return 1;
 
45
    }
 
46
  } while (!done);
 
47
  XML_ParserFree(parser);
 
48
  return 0;
 
49
}