Parsing cdata using expat in C

Joined
Mar 27, 2012
Messages
1
Reaction score
0
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:

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top