S
Sridhar
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>Lawful interception</description>
<start>2000-01-01</start>
<noStop/>
</featureKey>
<featureKey id="F2/2">
<description>SS7 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>Lawful 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,*FEATUREKEY_INFO;
static void XMLCALL startElement(void *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->attrElementName[word],"%s",
*atts++);
sprintf(F->attrData[word],"%s", *atts++);
if (strcmp(name, "description") == 0)
sprintf(F->elementDesc,"%s", *atts++);
word++;
}
F->numberAttr = word;
}
if (strcmp(name, "description") == 0)
word=0;
if (strcmp(name, "capacityKey") == 0)
{
C->found = 1;
strcpy(C->elementName, name);
while(*atts) /* element */
{
sprintf(C->attrElementName[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(void *userData, const XML_Char *s, int
len)
{
int i;
int currentAttr;
FEATUREKEY_INFO pMy = userData;
if (pMy->found == 1)
{
printf("\n\nelementName: %s", pMy->elementName);
for (i = 0; i < len; i++)
{
printf("%c", s);
}
printf("NumberATTR: %d \n", pMy->numberAttr);
for (currentAttr = 0; currentAttr < pMy->numberAttr;
currentAttr++)
{
printf("attrElementName %d: %s", currentAttr,
pMy->attrElementName[currentAttr]);
printf("\nattrData %d: %s\n", currentAttr,
pMy->attrData[currentAttr]);
printf("\nelementDesc %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_ParserCreate(NULL);
int done;
pMyData = malloc(sizeof(FEATUREKEY));
pMyData->found = 0;
pMyData->numberAttr = 0;
XML_SetUserData(parser, pMyData);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, 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(parser, buf, len, done) ==
XML_STATUS_ERROR)
{
printf("%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNum
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
(e-mail address removed)
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>Lawful interception</description>
<start>2000-01-01</start>
<noStop/>
</featureKey>
<featureKey id="F2/2">
<description>SS7 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>Lawful 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,*FEATUREKEY_INFO;
static void XMLCALL startElement(void *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->attrElementName[word],"%s",
*atts++);
sprintf(F->attrData[word],"%s", *atts++);
if (strcmp(name, "description") == 0)
sprintf(F->elementDesc,"%s", *atts++);
word++;
}
F->numberAttr = word;
}
if (strcmp(name, "description") == 0)
word=0;
if (strcmp(name, "capacityKey") == 0)
{
C->found = 1;
strcpy(C->elementName, name);
while(*atts) /* element */
{
sprintf(C->attrElementName[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(void *userData, const XML_Char *s, int
len)
{
int i;
int currentAttr;
FEATUREKEY_INFO pMy = userData;
if (pMy->found == 1)
{
printf("\n\nelementName: %s", pMy->elementName);
for (i = 0; i < len; i++)
{
printf("%c", s);
}
printf("NumberATTR: %d \n", pMy->numberAttr);
for (currentAttr = 0; currentAttr < pMy->numberAttr;
currentAttr++)
{
printf("attrElementName %d: %s", currentAttr,
pMy->attrElementName[currentAttr]);
printf("\nattrData %d: %s\n", currentAttr,
pMy->attrData[currentAttr]);
printf("\nelementDesc %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_ParserCreate(NULL);
int done;
pMyData = malloc(sizeof(FEATUREKEY));
pMyData->found = 0;
pMyData->numberAttr = 0;
XML_SetUserData(parser, pMyData);
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, 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(parser, buf, len, done) ==
XML_STATUS_ERROR)
{
printf("%s at line %d\n",
XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNum
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
(e-mail address removed)