Expat API

Cooper, Clark (CORP, Consultant) Clark.Cooper at corporate.ge.com
Tue Mar 2 23:04:11 GMT 1999


Dave Slotter <slotter at maya.com> wrote:
> My question is: where is the documentation on how to use the expat 
> API? I downloaded version 1.0.2 and ported the code to run the sample 
> program on my Macintosh, but I'm pretty much dead in the water

As far as I know the include file is the documentation. Expat is used by the
perl
module XML::Parser, which I maintain, but if you're having trouble with just
the
include file, you'd be absolutely lost looking at Expat.xs (I get lost
looking at it
sometimes). If you can use perl, I'd like to suggest XML::Parser as a
kindler,
gentler interface to expat.

If you're not a perl kinda fella, here's a small example of using expat:

#include "xmlparse.h"
#include <strings.h>
#include <stdio.h>

#define MAXLEV 512
#define BUFSIZE 4096

char indent[(MAXLEV + 1) * 2];
int level = 0;

void
start(void *data, const XML_Char *name, const XML_Char **atts)
{
  int offset;

  printf("\n%s> %s", indent, name);
  while (*atts) {
    printf(" %s='%s'", atts[0], atts[1]);
    atts += 2;
  }
  if (level >= MAXLEV) {
    fprintf(stderr, "Exceeded max level\n");
    exit(-1);
  }
  offset = level * 2;
  indent[offset]     = ' ';
  indent[offset + 1] = ' ';
  indent[offset + 2] = '\0';
  level++;
}  /* End start handler */

void
end(void *data, const XML_Char *name)
{
  level--;
  indent[level*2] = '\0';
  printf("\n%s< %s\n", indent, name);
}  /* End end handler */

void
text(void *data, const XML_Char *txt, int len)
{
  int i;

  printf("%s- ", indent);
  for (i = 0; i < len; i++)
    putchar(txt[i]);
}  /* End text handler */

void
main(int argc, char **argv)
{
  XML_Parser  prs;
  int stat;
  FILE * doc;

  if (argc < 2) {
    fprintf(stderr, "No filename supplied\n");
    exit(-1);
  }

  doc = fopen(argv[1], "r");
  if (! doc) {
    fprintf(stderr, "Couldn't open %s\n", argv[1]);
    exit(-1);
  }

  indent[0] = '\0';
  prs = XML_ParserCreate(NULL);
  XML_SetElementHandler(prs, start, end);
  XML_SetCharacterDataHandler(prs, text);

  while (! feof(doc)) {
    int cnt;
    void *buff = XML_GetBuffer(prs, BUFSIZE);
    if (! buff) {
      fprintf(stderr, "Ran out of memory\n");
      exit(-1);
    }
    cnt = fread(buff, 1, BUFSIZE, doc);
    stat = XML_ParseBuffer(prs, cnt, 0);
    if (! stat) {
      fprintf(stderr, "Parse error at line %d, column %d\n",
              XML_GetCurrentLineNumber(prs),
XML_GetCurrentColumnNumber(prs));
      exit(-1);
    }
  }
  fclose(doc);
  stat = XML_ParseBuffer(prs, 0, 1);
  if (! stat) {
    fprintf(stderr, "Parse error at line %d, column %d\n",
            XML_GetCurrentLineNumber(prs), XML_GetCurrentColumnNumber(prs));
    exit(-1);
  }
}  /* End main */

--
Clark Cooper                  Logic Technologies,Inc
cccooper at ltionline.com
(518) 388-7451                650 Franklin St., Suite 304
coopercc at netheaven.com
	                                  Schenectady, NY  12305

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)




More information about the Xml-dev mailing list