Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 138791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:19:12+00:00 2026-05-11T07:19:12+00:00

I have managed to parse ok. But now I am having trouble getting the

  • 0

I have managed to parse ok. But now I am having trouble getting the values that I need. I can get the element and the attributes. But cannot get the values. I would like to get the value of frame in this xml it is 20.

/* track the current level in the xml tree */ static int depth = 0; /* 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) { depth--; } int parse_xml(char *buff, size_t buff_size) {     FILE *fp;     fp = fopen('start_indication.xml', 'r');     if(fp == NULL)     {     printf('Failed to open file\n');     return 1;     }      XML_Parser parser = XML_ParserCreate(NULL);     int done;     XML_SetElementHandler(parser, start_element, end_element);      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; }    <data>     <header length='4'>             <item name='time' type='time'>16</item>             <item name='ref' type='string'>3843747</item>             <item name='port' type='int16'>0</item>             <item name='frame' type='int16'>20</item>     </header> </data>  Output from parsing   Element: data Element: header length= '4' Element: item name= 'time' type= 'time' Element: item name= 'ref' type= 'string' Element: item name= 'port' type= 'int16' Element: item name= 'frame' type= 'int16' 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:19:12+00:00Added an answer on May 11, 2026 at 7:19 am

    It is quite difficult with expat. expat is better when you are only interested with the structure, not the content of the elements. Why not using libxml instead? What are your reasons for using an even-based parser like expat, rather than a tree-based one?

    Anyway, the way to do it is to set a character data handler. Here is an example, based on your code:

    #include <expat.h> #include <stdio.h> #include <string.h>  #define BUFFER_SIZE 100000  /* track the current level in the xml tree */ static int      depth = 0;  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;     for (i = 0; i < depth; i++) {         printf(' ');     }     printf('Content of element %s was \'%s\'\n', el, last_content);     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('start_indication.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);     printf('Result is %i\n', result);     return 0; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem understanding the shift/reduce confict for a grammar that I know
I know there have been a lot of topics like this but I just
I have a python script which should parse a file and produce some output
Just started with JavaCC. But I have a strange behaviour with it. I want
I am writing a custom textfile-data parser (JSON-like) and I have lost many hours
This is a slightly.. vain question, but BuildBot's output isn't particularly nice to look
I parse an xml file containing books, for each new node I go: Book
I'm trying to use: // this is a BreakHistory class from the ADO.NET Data
I'm involved with a project using DotNetNuke version 05.01.04 Community Edition . We are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.