New to Expat - Help Required

Discussion in 'XML' started by Sridhar, Jan 5, 2005.

  1. Sridhar

    Sridhar Guest

    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
    Sridhar, Jan 5, 2005
    #1
    1. Advertising

  2. 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
    Karl Waclawek, Jan 5, 2005
    #2
    1. Advertising

  3. Sridhar

    Arjun Ray Guest

    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.
    Arjun Ray, Jan 6, 2005
    #3
  4. Sridhar

    Arjun Ray Guest

    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.
    Arjun Ray, Jan 6, 2005
    #4
  5. Sridhar

    Sridhar Guest

    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
    Sridhar, Jan 7, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    1
    Views:
    476
    Patrick TJ McPhee
    Dec 15, 2003
  2. Sridhar

    New to Expat

    Sridhar, Dec 29, 2004, in forum: XML
    Replies:
    1
    Views:
    426
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Dec 29, 2004
  3. Replies:
    0
    Views:
    780
  4. Will Stuyvesant

    Help with xml.parsers.expat please?

    Will Stuyvesant, Jul 4, 2003, in forum: Python
    Replies:
    1
    Views:
    670
    Alan Kennedy
    Jul 4, 2003
  5. sharan
    Replies:
    1
    Views:
    709
    Pavel Lepin
    Oct 26, 2007
Loading...

Share This Page