Parsing cdata using expat in C

Discussion in 'C Programming' started by Sohni, Mar 27, 2012.

  1. Sohni

    Sohni

    Joined:
    Mar 27, 2012
    Messages:
    1
    Hello,
    I m parsing XML in C using following code

    Code:
    #include <expat.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define BUFFER_SIZE 100000  /* track the current level in the xml tree */
    
    static int depth = 0, j = 1;
    static char *last_content;  /* first when start element is encountered */
    
    void start_element(void *data, const char *element, const char **attribute) {
            int i;
            for (i = 0; i < depth; i++) {
                    printf("  ");
            }
            printf(":%s", element);
            for (i = 0; attribute[i]; i += 2) {
                    printf("%s= '%s'", attribute[i], attribute[i + 1]);
            }
            printf("\n");
            depth++;
    }  /* decrement the current level of the tree */
    
    void end_element(void *data, const char *el) {
            int i;
            const char *String = "Sendmsg_XML_";
            char *temp = (char*)malloc( strlen( String ) + 10 + 1 ); /*assume int can be printed in 10 chars */
     for (i = 0; i < depth; i++) {
                    printf("  ");
            }
                    depth--;
      }
    
    void handle_data(void *data, const char *content, int length) {
            char *tmp = malloc(length);
            strncpy(tmp, content, length);
            tmp[length] = '\0';
            data = (void *) tmp;
            last_content = tmp;         /* TODO: concatenate the text nodes? */
    }
    int parse_xml(char *buff, size_t buff_size) {
            FILE *fp;
            fp = fopen("sos.xml", "r");
            if (fp == NULL) {
                    printf("Failed to open file\n");
                    return 1;
            }
            XML_Parser parser = XML_ParserCreate(NULL);
            XML_SetElementHandler(parser, start_element, end_element);
            XML_SetCharacterDataHandler(parser, handle_data);
            memset(buff, 0, buff_size);
            printf("strlen(buff) before parsing: %d\n", strlen(buff));
            size_t file_size = 0;
            file_size = fread(buff, sizeof(char), buff_size, fp);  /* parse the xml */
            if (XML_Parse(parser, buff, strlen(buff), XML_TRUE) == XML_STATUS_ERROR)
            {
                    printf("Error: %s\n", XML_ErrorString(XML_GetErrorCode(parser)));
            }
            fclose(fp);
            XML_ParserFree(parser);
            return 0;
    }
    
    int main(int argc, char **argv) {
            int  result;
            char buffer[BUFFER_SIZE];
            result = parse_xml(buffer, BUFFER_SIZE);
            return 0;
    }
    
    but my code is not successfully parsing cdata of XML file "<![CDATA[hjjhgjh]]>". It read one line of cdata when i put that in "<cdata></cdata>" tags.

    Can anybody please help me in parsing cdata in "<![CDATA[hjjhgjh]]>" format having multiple lines inside it?
    Thanks in advance.
     
    Last edited: Mar 27, 2012
    Sohni, Mar 27, 2012
    #1
    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. John Davison
    Replies:
    1
    Views:
    584
    Hal Rosser
    Jul 7, 2004
  2. Fabian Kr?ger
    Replies:
    0
    Views:
    409
    Fabian Kr?ger
    Nov 19, 2003
  3. Chris Waddingham

    expat whitespace in CDATA sections

    Chris Waddingham, Mar 3, 2004, in forum: XML
    Replies:
    0
    Views:
    496
    Chris Waddingham
    Mar 3, 2004
  4. troppfigo@excite.it
    Replies:
    3
    Views:
    756
    Joe Kesselman
    Mar 6, 2006
  5. sharan
    Replies:
    1
    Views:
    721
    Pavel Lepin
    Oct 26, 2007
Loading...

Share This Page