473,320 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

New to Expat - Help Required

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[i]);
}
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
ms****@yahoo.com

Jul 20 '05 #1
4 1861
Sridhar wrote:
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>
--------------------------------------------------------------------------------- <snip>
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
Jul 20 '05 #2
On Wed, 05 Jan 2005 04:39:42 -0800, Sridhar wrote:
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.

Jul 20 '05 #3
On Thu, 06 Jan 2005 01:34:02 +0000, Arjun Ray wrote:
printf( "\tAND ATTRIBUTES of [%s] are:\n" ) ;


That should be

printf( "\tAND ATTRIBUTES of [%s] are:\n", name ) ;

Sorry about that.

Jul 20 '05 #4
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
ms****@yahoo.com

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Brown | last post by:
Python 2.3 comes with its own version of Expat. setup.py says it is Expat 1.95.2, but the code in Modules/expat seems to actually be Expat 1.95.6. It's really 1.95.6, right? -- Mike
1
by: Doug | last post by:
I am running solaris 7 on two machines. I compiled python 2.2.1 with expat parser on one machine. The python binary is located in /usr/local/bin and the libraries are located in /usr/local/lib. ...
1
by: Ingo Blank | last post by:
Hi, while 95% of my 'psycoed' applications run fine, it throws SIGSEGVs in conjunction with expat. Anybody noticed the same ? Python 2.3.2 (#4, Nov 13 2003, 02:10:49) on linux2 $ uname...
2
by: Thomas Guettler | last post by:
Hi! What are the difference between xml.parsers.expat and xml.sax? Up to now I used xml.sax.make_parser and subclass from ContentHandler. I think xml.sax.make_parser uses expat as default....
4
by: alainpoint | last post by:
Hello, I use Elementtree to parse an elementary SVG file (in fact, it is one of the examples in the "SVG essentials" book). More precisely, it is the fig0201.svg file in the second chapter. The...
1
by: David Madore | last post by:
Hi! Anyone in for a Byzantine discussion on XML well-formedness? Here's the situation: test.xml contains --- test.xml: cut after --- <?xml version="1.0" encoding="us-ascii"?> <!DOCTYPE...
1
by: B Mahoney | last post by:
I have a simple Kid template document: <?xml version='1.0' encoding='utf-8'?> <?python import time title = "A Kid &anything; Template" ?> <html xmlns="http://www.w3.org/1999/xhtml"...
2
by: dwelch91 | last post by:
Hi, c.l.p.'ers- I am having a problem with the import of xml.parsers.expat that has gotten me completely stumped. I have two programs, one a PyQt program and one a command line (text) program...
1
by: vadlapatlahari | last post by:
Hi, I get the following error with Expat while configuring my application server. Can anyone suggest a solution? When i do an ldd, i get the following : $ldd Expat.so Expat.so needs:...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.