Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Parsing cdata using expat in C

Reply
Thread Tools

Parsing cdata using expat in C

 
 
Sohni Sohni is offline
Junior Member
Join Date: Mar 2012
Posts: 1
 
      03-27-2012
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 by Sohni; 03-27-2012 at 11:20 AM..
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want help on how we convert output to tabular format Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. sharan XML 1 10-26-2007 01:20 PM
Using the expat parser (http://expat.sourceforge.net/) i have to parse the following xml file and print it on the screen in tabular format. Want a c program on that! sharan XML 1 10-26-2007 07:56 AM
Can I un-CDATA my CDATA section and elaborate a transformation for the contained data? troppfigo@excite.it XML 3 03-06-2006 03:01 AM
Extracting CDATA Text without CDATA Tags??? John Davison Java 1 07-06-2004 11:00 PM
expat whitespace in CDATA sections Chris Waddingham XML 0 03-03-2004 11:56 AM



Advertisments