#!/bin/ch /* Copyright (c) Oracle Corporation 1999. All Rights Reserved. */ /** ** This file demonstrates a simple use of the Namespace extensions to ** the SAX APIs. **/ #include "xdkkit.h" #include #ifndef ORATYPES # include #endif #ifndef ORAXML_ORACLE # include #endif #define DOCUMENT "c:/Inetpub/wwwroot/chxml/demos/xdk/NSExample.xml" /*------------------------------------------------------------------------ FUNCTION PROTOTYPES ------------------------------------------------------------------------*/ static int sax_startdocument(void *ctx); static int sax_enddocument(void *ctx); static int sax_endelement(void *ctx, const oratext *name); static int sax_nsstartelement(void *ctx, const oratext *qname, const oratext *local, const oratext *namespace, const struct xmlnodes *attrs); /* SAX callback structure */ xmlsaxcb sax_callback = { sax_startdocument, sax_enddocument, 0, sax_endelement, 0, 0, 0, 0, 0, sax_nsstartelement, 0, 0, 0, 0, 0, 0, 0, 0 }; /* SAX callback context */ typedef struct { xmlctx *ctx; uword depth; } cbctx; /*------------------------------------------------------------------------ MAIN ------------------------------------------------------------------------*/ int main() { xmlctx *ctx; uword i; oratext *doc, *encoding; xmlsaxcb *saxcb; cbctx saxctx; void *saxcbctx; ub4 flags; uword ecode; Response.setContentType(contenttype); Response.begin(); //Response.title(title); doc = encoding = (oratext *)0; flags = XML_FLAG_VALIDATE | XML_FLAG_DISCARD_WHITESPACE; doc = (oratext *)DOCUMENT; /* set up SAX callbacks */ saxcb = &sax_callback; saxcbctx = (void *) &saxctx; /* initialize LPX context */ if (!(ctx = xmlinit(&ecode, encoding, (void (*)(void *, const oratext *, uword)) 0, (void *) 0, saxcb, saxcbctx, (const xmlmemcb *) 0, (void *) 0, (const oratext *) 0))) { printf("Failed to initialize XML parser, error %u\n", (unsigned) ecode); return -1; } /* parse the document */ printf("\nParsing '%s' ...\n", doc); ecode = xmlparse(ctx, doc, encoding, flags); if (ecode) printf("\nParse failed, code %u\n", (unsigned) ecode); else printf("\nParse succeeded.\n"); /* terminate */ (void) xmlterm(ctx); Response.end(); return (ecode ? -1 : 0); } /*------------------------------------------------------------------------ SAX Interface ------------------------------------------------------------------------*/ static int sax_startdocument(void *ctx) { printf("\nStartDocument\n\n"); return 0; } static int sax_enddocument(void *ctx) { printf("\nEndDocument\n"); return 0; } static int sax_endelement(void *ctx, const oratext *name) { printf("\nELEMENT Name : %s\n", name); return 0; } static int sax_nsstartelement(void *ctx, const oratext *qname, const oratext *local, const oratext *namespace, const struct xmlnodes *attrs) { cbctx *saxctx = (cbctx *) ctx; xmlnode *attr; size_t i; const oratext *aqname; const oratext *aprefix; const oratext *alocal; const oratext *anamespace; const oratext *avalue; /* * Use the functions getXXXQualifiedName(), getXXXLocalName(), and * getXXXNamespace() to get Namespace information. */ if (qname == (oratext*)NULL) qname = (oratext*)" "; if (local == (oratext*)NULL) local = (oratext*)" "; if (namespace == (oratext*)NULL) namespace = (oratext*)" "; printf("ELEMENT Qualified Name: %s\n", qname); printf("ELEMENT Local Name : %s\n", local); printf("ELEMENT Namespace : %s\n", namespace); if (attrs) { for (i = 0; i < numAttributes(attrs); i++) { attr = getAttributeIndex(attrs, i); aqname = aprefix = alocal = anamespace = avalue = (oratext*)" "; if (getAttrQualifiedName(attr) != (oratext*)NULL) aqname = getAttrQualifiedName(attr); if (getAttrPrefix(attr) != (oratext*)NULL) aprefix = getAttrPrefix(attr); if (getAttrLocal(attr) != (oratext*)NULL) alocal = getAttrLocal(attr); if (getAttrNamespace(attr) != (oratext*)NULL) anamespace = getAttrNamespace(attr); if (getAttrValue(attr) != (oratext*)NULL) avalue = getAttrValue(attr); printf(" ATTRIBUTE Qualified Name : %s\n", aqname); printf(" ATTRIBUTE Prefix : %s\n", aprefix); printf(" ATTRIBUTE Local Name : %s\n", alocal); printf(" ATTRIBUTE Namespace : %s\n", anamespace); printf(" ATTRIBUTE Value : %s\n", avalue); printf("\n"); } } return 0; }