0

I am able to parse XML successfully, but I am facing probelm while extracting data from CDATA. For this, I am using XML_SetCdataSectionHandler(parser,CDATAstart,CDATAend). And by using this, my program can identify where the cdata exists but I am not able to get inside data from that cdata.I don't know how to code inside CDATAstart and CDATAend to extract the data. Can someone please help me in doing this. My xml parser is shown below

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);
    XML_SetCdataSectionHandler(parser,CDATAstart,CDATAend);
    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;
}
skaffman
  • 398,947
  • 96
  • 818
  • 769

1 Answers1

0

The CdataSectionHandlers only tell you when a CDATA section starts and ends, the actual data is delivered through the CharacterDataHandler (or, if omitted, the DefaultHandler).

hroptatyr
  • 4,702
  • 1
  • 35
  • 38