-
Readme
-
Installation
-
API Docs
-
Samples
-
Programming
-
License
|
C: DOMCount
The Code: qcsrc.DOMCount
The lines of code that correspond to XML parser initialization, use and clean-up are displayed in blue.
/********************************************************************
*DOMCount -- This sample program uses the DOM parser and
* prints out the number of elements.
* The xml file is input by the user.
********************************************************************
*/
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include "qxml4pr400.h"
#define true 1
#define false 0
/* ---------------------------------------------------------------------------
* This is a simple program which invokes the DOMParser to build a DOM
* tree for the specified input file. It then walks the tree and counts
* the number of elements. The element count is then printed.
* ---------------------------------------------------------------------------
*/
/* ------------------------- FUNCTION PROTOTYPE ---------------------------------*/
void usage(void);
void display_init_msg(int a);
void trim_msg (char string[], int length);
/* ------------------------- END FUNCTION PROTOTYPE -----------------------------*/
/* ------------------------- HELPER FUNCTIONS -----------------------------------*/
/* This funtion will trim any blanks tabs and new line from the error message, */
void trim_msg (char string[], int length)
{
int n;
for ( n = length-1; n >0; n--)
if (string[n] != ' ' && string[n] !='\t' && string[n] !='\n')
break;
string[n+1] ='\0';
}
/* This will display the usage of DOMCount */
void usage()
{
printf("\nUsage:\n");
printf(" DOMCount [-v -n] {XML file}\n\n");
printf(" This program invokes the XML4PR DOM parser, builds\n");
printf(" the DOM tree, and then prints the number of elements\n");
printf(" found in the input XML file.\n\n");
printf(" Options:\n");
printf(" -v=xxx Validation scheme [always | never | auto*]\n");
printf(" -n Enable namespace processing. Defaults to off.\n");
printf(" -s Enable schema processing. Default is off.\n");
printf(" -f Enable full schema constraint checking. Defaults to off.\n\n");
printf(" -n Enable namespace processing. Defaults to off.\n\n");
printf(" * = Default if not provided explicitly\n\n");
}
static char ibmid[] = "Copyright IBM Corporation 2001 LICENSED MATERIAL - PROGRAM PROPERTY OF IBM";
QxmlXML_env_t xml_env; /* Variable for XML environment error message and return code */
QxmlParser_env_t p_env; /* Variable for XML Parser environment error message, return code, column , and line number */
int main (int argc, char *argv[])
{
char* xmlFile = 0;
int valScheme = QxmlAUTO_VALIDATE;
int doNamespaces = false;
int doSchema = false;
int schemaFullChecking = false;
int argInd ;
int getSawErrors = false;
int len;
long duration;
unsigned int elementCount ;
clock_t t1,t2;
DOMParser parser;
QxmlInit(&xml_env); /* Initialize the XML env. and verify success*/
if (xml_env.rtncode != Qxml_DOMNOERROR)
{
printf("Initialization failed with return code %s\n", xml_env.rtncode );
return 1;
}
if (argc < 2)
{
usage();
QxmlTerm();
return 1;
}
/* See if non validating dom parser configuration is requested. */
if ((argc == 2) && !strcmp(argv[1], "-?"))
{
usage();
QxmlTerm();
return 2;
}
for (argInd = 1; argInd < argc; argInd++)
{
/* Break out on first non-dash parameter */
if (argv[argInd][0] != '-')
break;
if (!strncmp(argv[argInd], "-v=", 3) || !strncmp(argv[argInd], "-V=", 3))
{
const char* const parm = &argv[argInd][3];
if (!strcmp(parm, "never"))
valScheme = QxmlNEVER_VALIDATE;
else if (!strcmp(parm, "auto"))
valScheme = QxmlAUTO_VALIDATE;
else if (!strcmp(parm, "always"))
valScheme = QxmlALWAYS_VALIDATE;
else
{
printf("Unknown -v= value: \t %s\n", parm );
return 2;
}
}
else
if ((!strcmp(argv[argInd], "-n")||!strcmp(argv[argInd], "-N")))
doNamespaces = true;
else
if ((!strcmp(argv[argInd], "-s")||!strcmp(argv[argInd], "-S")))
doSchema = true;
else
if ((!strcmp(argv[argInd], "-f")||!strcmp(argv[argInd], "-F")))
schemaFullChecking = true;
else
printf("Unknown option, %s , ignoring it\n ", argv[argInd]);
}
/* There should be only one and only one parameter left, and that
should be the file name.
*/
if (argInd != argc - 1)
{
usage();
QxmlTerm();
return 1;
}
xmlFile = argv[argInd];
/* Instantiate the DOM parser providing a structure for the return of DOM parser errors.*/
parser = QxmlDOMParser_new(&p_env);
/* set appropriate validation */
QxmlDOMParser_setValidationScheme(parser, valScheme);
QxmlDOMParser_setDoNamespaces(parser, doNamespaces);
QxmlDOMParser_setDoSchema(parser, doSchema);
QxmlDOMParser_setValidationSchemaFullChecking(parser, schemaFullChecking);
/*
Get the starting time and kick off the parse of the indicated
file. Catch any exceptions that might propogate out of it.
*/
p_env.errortype = 0;
memset(p_env.errmsg, '\0',Qxml_MAXMSGSIZE);
/* This will parse the xmlfile and get the time used to do the parsing. */
t1=clock(); /* get the system time before parsing the file */
QxmlDOMParser_parse_SystemId(parser,xmlFile,Qxml_CCSID37,strlen((char*)xmlFile));
t2=clock(); /* get the system time after the parsing the file */
duration = ((long)(1000*((float)(t2-t1)/CLOCKS_PER_SEC))); /* get the total time used in sec. for parsing the file */
if (xml_env.rtncode !=Qxml_DOMNOERROR)
{
printf("An error occured using DOM apis\n ReturnCode:%d\n",xml_env.rtncode);
getSawErrors=true;
}
if (p_env.errortype !=Qxml_NOERROR) /*Error occurred during parse, print message */
{
char errmsg [Qxml_MAXMSGSIZE+1];
char datasrc_msg[Qxml_MAXFILENMSIZE+1];
memcpy(errmsg,p_env.errmsg,Qxml_MAXMSGSIZE-1);
memcpy(datasrc_msg,p_env.datasrc,Qxml_MAXFILENMSIZE-1);
errmsg[Qxml_MAXMSGSIZE]='\0';
datasrc_msg[Qxml_MAXFILENMSIZE]='\0';
trim_msg(errmsg, Qxml_MAXMSGSIZE);
printf("\n==================================================\n");
printf("Error occured...........: %s\n", errmsg);
printf("Line Number ............: %i\n", p_env.rtnline);
printf("Column Number ..........: %i\n", p_env.rtncol);
trim_msg(datasrc_msg,Qxml_MAXFILENMSIZE);
printf("At source file..........: %s\n",datasrc_msg);
printf("While Parsing XML file..: %s", xmlFile);
printf("\n==================================================\n");
getSawErrors=true;
return -1;
}
/*
* Extract the DOM tree, get the list of all the elements and report the
* length as the count of elements.
*/
if (getSawErrors)
{
printf("\nErrors occured, no output available\n");
}
else
{
char* star ="*"; /* indicate all elements*/
int stringID = Qxml_CCSID37;
DOM_Document doc = QxmlDOMParser_getDocument(parser);
DOM_NodeList nodeList = QxmlDOM_Document_getElementsByTagName(doc, star,stringID ,1) ;
elementCount = QxmlDOM_NodeList_getLength(nodeList);
/* Print out the stats that we collected and time taken.*/
printf(" %s : %li ms ( %u Elements).\n", xmlFile, duration, elementCount);
QxmlDOM_NodeList_delete(nodeList);
QxmlDOM_Document_delete(doc);
}
/* And call the termination method*/
QxmlDOMParser_delete(parser);
QxmlTerm();
return 0;
}
|