OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   RE: Expat API

[ Lists Home | Date Index | Thread Index ]
  • From: "Cooper, Clark (CORP, Consultant)" <Clark.Cooper@corporate.ge.com>
  • To: "'Dave Slotter'" <slotter@maya.com>
  • Date: Tue, 2 Mar 1999 18:02:47 -0500

Dave Slotter <slotter@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@ltionline.com
(518) 388-7451                650 Franklin St., Suite 304
coopercc@netheaven.com
	                                  Schenectady, NY  12305

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@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@ic.ac.uk the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo@ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa@ic.ac.uk)





 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS