#!/bin/ch /* Copyright (c) Oracle Corporation 1999. All Rights Reserved. */ /* NAME SAXSample.c - Sample SAX usage DESCRIPTION Sample usage of C XML parser via SAX interface */ #include "xdkkit.h" #include #ifndef ORATYPES # include #endif #ifndef ORAXML_ORACLE # include #endif #define DOCUMENT "c:/Inetpub/wwwroot/chxml/demos/xdk/cleo.xml" #define DEFAULT_KEYWORD "death" char *keyword; size_t keylen; oratext *elem; oratext speaker[80]; oratext *findsub(oratext *buf, size_t bufsiz, oratext *sub, size_t subsiz); void savestr(oratext *buf, oratext *s, size_t len); /* SAX callback functions */ sword startDocument(void *ctx); sword endDocument(void *ctx); sword startElement(void *ctx, const oratext *name, const struct xmlnodes *attrs); sword endElement(void *ctx, const oratext *name); sword characters(void *ctx, const oratext *ch, size_t len); xmlsaxcb saxcb = { startDocument, endDocument, startElement, endElement, characters }; int main(int argc, char **argv) { xmlctx *ctx; ub4 flags; uword ecode; flags = XML_FLAG_VALIDATE | XML_FLAG_DISCARD_WHITESPACE; Response.setContentType(contenttype); Response.begin(); //Response.title(title); puts("XML C SAX sample"); keyword = (argc > 1) ? argv[1] : DEFAULT_KEYWORD; keylen = strlen(keyword); puts("Initializing XML package..."); if (!(ctx = xmlinit(&ecode, (const oratext *) 0, (void (*)(void *, const oratext *, uword)) 0, (void *) 0, &saxcb, (void *) 0, (const xmlmemcb *) 0, (void *) 0, (const oratext *) 0))) { (void) printf("Failed to initialize XML parser, error %u\n", (unsigned) ecode); return 1; } printf("Parsing '%s' and looking for lines containing '%s'...\n", DOCUMENT, keyword); elem = (oratext *) ""; if (ecode = xmlparse(ctx, (oratext *) DOCUMENT, (oratext *) 0, flags)) return 1; (void) xmlterm(ctx); /* terminate XML package */ Response.end(); return 0; } sword startDocument(void *ctx) { puts("startDocument"); return 0; } sword endDocument(void *ctx) { puts("endDocument"); return 0; } sword startElement(void *ctx, const oratext *name, const struct xmlnodes *attrs) { elem = (oratext *) name; return 0; } sword endElement(void *ctx, const oratext *name) { elem = (oratext *) ""; return 0; } sword characters(void *ctx, const oratext *ch, size_t len) { if (!strcmp((char *) elem, "SPEAKER")) savestr(speaker, (oratext *) ch, len); else if (findsub((oratext *) ch, len, (oratext *) keyword, keylen)) printf(" %s: %.*s\n", speaker, len, ch); return 0; } oratext *findsub(oratext *buf, size_t bufsiz, oratext *sub, size_t subsiz) { uword i; if (!buf || !bufsiz || (subsiz > bufsiz)) return (oratext *) 0; if (!sub || !subsiz) return buf; for (i = 0; i < bufsiz - subsiz; i++, buf++) { if (!memcmp(buf, sub, subsiz)) return buf; } return (oratext *) 0; } void savestr(oratext *buf, oratext *s, size_t len) { memcpy(buf, s, len); buf[len] = 0; } /* End of SAXSample.c */