New to Expat - Help Required

Sridhar (4)

Usenet Archive
Joined
Dec 29, 2004
Messages
3
Reaction score
0
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
 

Karl Waclawek

Usenet Archive
Joined
Nov 10, 2001
Messages
12
Reaction score
0
Sridhar said:
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 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.

I don't understand your question.
The only attributes in your XML file are named "id".

Karl
 

Arjun Ray

Usenet Archive
Joined
Jan 22, 2000
Messages
62
Reaction score
0
typedef struct
{
int found;
char elementName[10];
char attrElementName[3][10];
char attrData[3][20];
char elementDesc[30];
int numberAttr;
} FEATUREKEY,*FEATUREKEY_INFO;
sprintf(F->attrElementName[word],"%s", *atts++);

I hope you won't do something quite so dangerous in any kind of serious program. ;-)
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.

You are thoroughly confused about elements, attributes, and "ids", so your description of your problem makes no sense. You should read the XML spec to learn the difference between elements and attributes.

You can gain further insight by studying the output of these handlers in place of yours:

static void XMLCALL charHandler(void *userData, const XML_Char *s, int len) { printf( "DATA passed to charHandler (%d characters):\n", len ) ; printf( "==>%*s", len, s ) ; printf( "\nEND OF DATA (%d characters)\n", len ) ; }

static void XMLCALL startElement(void *userData, const char *name, const char **atts) { const char *np ; const char *vp ;

puts( "ENTERING startElement handler!!!" ) ;
printf( "\tELEMENT at this point is: [%s]\n", name ) ;
printf( "\tAND ATTRIBUTES of [%s] are:\n" ) ;
while ( *atts ) {
np = *atts++ ;
vp = *atts ;
if ( vp ) {
printf( "\t\t[%s] ==> [%s]\n", np, vp ) ;
atts++ ;
}
}
puts( "RETURNING from startElement handler!!!" ) ;
}

static void XMLCALL endElement(void *userData, const char *name) { printf( "End of ELEMENT [%s] reported by parser!!!\n", name ) ; }

In particular, observe *how many times* startElement and endElement are called, and at what points in relation to your XML input file.
 

Sridhar (4)

Usenet Archive
Joined
Dec 29, 2004
Messages
3
Reaction score
0
HI,

I am sorry to confused a lot, but I am new to these environments (XML and Expat). Acutally I need to extract information in <featureKey> & </featureKey> tags (HTML Word) put in a data structure. In this case 2 records of information one for each <featureKey> & </featureKey> set. Please let me know how to do this using Expat.

<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>

I hope now clearly explained the problem.
Thanks and Regards,
Sridhar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top