#!/bin/ch /* Copyright (c) Oracle Corporation 1999. All Rights Reserved. */ /** ** This file demonstates a simple use of the parser and Namespace ** extensions to the DOM APIs. ** The XML file that is given to the application is parsed and the ** elements and attributes in the document are printed. **/ #include "xdkkit.h" #ifndef ORATYPES # include #endif #ifndef ORAXML_ORACLE # include #endif #define DOCUMENT "c:/Inetpub/wwwroot/chxml/demos/xdk/NSExample.xml" /*------------------------------------------------------------------------ FUNCTION PROTOTYPES ------------------------------------------------------------------------*/ static void DOMNSprint(xmlctx *ctx); static void printElements(xmlctx *ctx, xmlnode *n); static void printAttrs(xmlctx *ctx, xmlnode *n); /*------------------------------------------------------------------------ MAIN ------------------------------------------------------------------------*/ int main() { Response.setContentType(contenttype); Response.begin(); //Response.title(title); xmlctx *ctx; oratext *encoding, *doc; void *saxcbctx; const xmlsaxcb *saxcb; uword ecode; ub4 flags; encoding = doc = (oratext *) 0; saxcbctx = (void *) 0; saxcb = (const xmlsaxcb *) 0; flags = XML_FLAG_VALIDATE | XML_FLAG_DISCARD_WHITESPACE; doc = (oratext *)DOCUMENT; /* 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("Parse failed, code %u\n", (unsigned) ecode); else printf("Parse succeeded.\n"); /* print results */ printf("Printing results ...\n"); DOMNSprint(ctx); /* terminate */ (void) xmlterm(ctx); Response.end(); return (ecode ? -1 : 0); } /*------------------------------------------------------------------------ DOMNSprint ------------------------------------------------------------------------*/ static void DOMNSprint(xmlctx *ctx) { xmlnode *root; root = getDocumentElement(ctx); printf("\nThe elements are:\n"); printElements(ctx, root); } /*------------------------------------------------------------------------ printElements ------------------------------------------------------------------------*/ static void printElements(xmlctx *ctx, xmlnode *n) { xmlnodes *nodes; uword i; size_t nn; const oratext *qname; const oratext *namespace; const oratext *local; const oratext *prefix; if (n == (xmlnode*)NULL) return; if (nodes = getChildNodes(n)) { for (nn = numChildNodes(nodes), i = 0; i < nn; i++) { /* get node qualified name, local name, namespace, and prefix */ qname = namespace = local = prefix = (oratext*)" "; if (getNodeQualifiedName(n) != (oratext*)NULL) qname = getNodeQualifiedName(n); if (getNodePrefix(n) != (oratext*)NULL) prefix = getNodePrefix(n); if (getNodeLocal(n) != (oratext *)NULL) local = getNodeLocal(n); if (getNodeNamespace(n) != (oratext*)NULL) namespace = getNodeNamespace(n); printf(" ELEMENT Qualified Name: %s\n", qname); printf(" ELEMENT Prefix Name : %s\n", prefix); printf(" ELEMENT Local Name : %s\n", local); printf(" ELEMENT Namespace : %s\n", namespace); printAttrs(ctx, n); printElements(ctx, (xmlnode *) getChildNode(nodes, i)); } } return; } /*------------------------------------------------------------------------ printAttrs ------------------------------------------------------------------------*/ static void printAttrs(xmlctx *ctx, xmlnode *n) { xmlnodes *attrs; xmlnode *a; uword i; size_t na; const oratext *value; const oratext *qname; const oratext *namespace; const oratext *local; const oratext *prefix; if (attrs = getAttributes(n)) { printf("\n ATTRIBUTES: \n"); for (na = numAttributes(attrs), i = 0; i < na; i++) { /* get attr qualified name, local name, namespace, and prefix */ a = getAttributeIndex(attrs, i); qname = namespace = local = prefix = value = (oratext*)" "; if (getAttrQualifiedName(a) != (oratext*)NULL) qname = getAttrQualifiedName(a); if (getAttrNamespace(a) != (oratext*)NULL) namespace = getAttrNamespace(a); if (getAttrLocal(a) != (oratext*)NULL) local = getAttrLocal(a); if (getAttrPrefix(a) != (oratext*)NULL) prefix = getAttrPrefix(a); if (getAttrValue(a) != (oratext*)NULL) value = getAttrValue(a); printf(" %s = %s\n", qname, value); printf(" Namespace : %s\n", namespace); printf(" Local Name: %s\n", local); printf(" Prefix : %s\n\n", prefix); } } printf("\n"); }