HI All,
Currently writing a small program and here is my xml file and program.
---------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes "?>
<abc>
<featureKey id="F1/1">
<description>La wful interception</description>
<start>2000-01-01</start>
<noStop/>
</featureKey>
<featureKey id="F2/2">
<description>SS 7 Signaling Over IP</description>
<start>2000-01-01</start>
<stop>2000-01-01</stop>
</featureKey>
</abc>
---------------------------------------------------------------------------------
Here is my program
---------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include "expat.h"
#define XMLFILENAME "l.xml"
/*
<featureKey id="F1/1">
<description>La wful interception</description>
<start>2000-01-01</start>
<noStop/>
</featureKey>
*/
typedef struct
{
int found;
char elementName[10];
char attrElementName[3][10];
char attrData[3][20];
char elementDesc[30];
int numberAttr;
} FEATUREKEY,*FEA TUREKEY_INFO;
static void XMLCALL startElement(vo id *userData, const char *name,
const char **atts)
{
int word = 0;
FEATUREKEY_INFO F = userData;
FEATUREKEY_INFO C = userData;
if (strcmp(name, "featureKey ") == 0)
{
F->found = 1;
strcpy(F->elementName, name);
while(*atts) /* element */
{
sprintf(F->attrElementNam e[word],"%s",
*atts++);
sprintf(F->attrData[word],"%s", *atts++);
if (strcmp(name, "descriptio n") == 0)
sprintf(F->elementDesc,"% s", *atts++);
word++;
}
F->numberAttr = word;
}
if (strcmp(name, "descriptio n") == 0)
word=0;
if (strcmp(name, "capacityKe y") == 0)
{
C->found = 1;
strcpy(C->elementName, name);
while(*atts) /* element */
{
sprintf(C->attrElementNam e[word],"%s",
*atts++);
sprintf(C->attrData[word],"%s", *atts++);
word++;
}
C->numberAttr = word;
}
}
static void XMLCALL endElement(void *userData, const char *name)
{
}
static void XMLCALL charHandler(voi d *userData, const XML_Char *s, int
len)
{
int i;
int currentAttr;
FEATUREKEY_INFO pMy = userData;
if (pMy->found == 1)
{
printf("\n\nele mentName: %s", pMy->elementName) ;
for (i = 0; i < len; i++)
{
printf("%c", s[i]);
}
printf("NumberA TTR: %d \n", pMy->numberAttr);
for (currentAttr = 0; currentAttr < pMy->numberAttr;
currentAttr++)
{
printf("attrEle mentName %d: %s", currentAttr,
pMy->attrElementNam e[currentAttr]);
printf("\nattrD ata %d: %s\n", currentAttr,
pMy->attrData[currentAttr]);
printf("\neleme ntDesc %d: %s\n", currentAttr,
pMy->elementDesc) ;
}
pMy->found = 0;
}
}
int main(int argc, char *argv[])
{
FILE *xmlfile;
char buf[BUFSIZ];
FEATUREKEY_INFO pMyData;
XML_Parser parser = XML_ParserCreat e(NULL);
int done;
pMyData = malloc(sizeof(F EATUREKEY));
pMyData->found = 0;
pMyData->numberAttr = 0;
XML_SetUserData (parser, pMyData);
XML_SetElementH andler(parser, startElement, endElement);
XML_SetCharacte rDataHandler(pa rser, charHandler);
printf("Opening File\n");
if ((xmlfile = fopen (XMLFILENAME, "r")) == NULL)
{
printf("Error opening XML file\n");
return -1;
}
do
{
size_t len = fread(buf, 1, sizeof(buf), xmlfile);
done = len < sizeof(buf);
if (XML_Parse(pars er, buf, len, done) ==
XML_STATUS_ERRO R)
{
printf("%s at line %d\n",
XML_ErrorString (XML_GetErrorCo de(parser)), XML_GetCurrentL ineNum
ber(parser));
return 1;
}
} while (!done);
XML_ParserFree( parser);
fclose(xmlfile) ;
free(pMyData);
return 0;
}
-------------------------------------------------------------------
Here I am able to print and grab information of featureKey ids but not
able to get attached attributes of featurekey ids. Please let me know
how to do it.
Thanks and Regards,
Sridhar
ms****@yahoo.co m