Simple C to XML serializer 0.0.2 released in GitHub

By | February 12, 2012

Some while ago I wrote a simple C to XML serializer that reads C structs and unions from a header file a writes them to a XML file. You can read that post here.

I’ve improved it a little bit and released it in GitHub. You can check it out here.

This program is in its first stages and currently it only converts the most widely used C-structs constructs to XML. However, it could potentially serialize other C constructs and almost any C code since it uses an almost full-compatible C grammar.

The version I released in GitHub adds the support for structs/unions defined inside a macro.

I’ll try to explain how this feature works:
Suppose you have a file, test3.h, with a macro that defines the following struct:

#define def_struct_data(prefix,number,code_number)
struct prefix##driver_data
    task_status_t tsk_sts;

    struct buttons_platform_data pdata[number];
    struct prefix##an_event event;

    struct prefix##buttons_settings
        uint8_t thresholds[3];
        uint8_t pressed_threshold;
        struct prefix##buttons_mask hash_table[code_number];
    }__attribute__((packed)) settings;

    uint8_t raw[((number) / 8) + 1];
    uint8_t raw_mem[((number) / 8) + 1];


As you probably know, the syntax of a C macro is not valid for a C compiler, that’s one of the reasons for using the C pre-compiler: it converts the macro to something that the compiler understands.
In the SC2XML case, applies the same condition. Therefore, you have to create a very simple C file that includes the file that you want to serialize, in this case test3.h, along with the use of the macro and execute sc2xml with that file:

Assume that the name of your simple C file is test3.stub.h. It should looks like this:

#include "test3.h"

def_struct_data is the name of your macro.

Execute sc2xml:

$ sc2xml test3.stub.h

sc2xml 0.0.2

INFO: *** Parsing file data/test3.gen.h ***

The final generated XML file, test3.h.xml, will look like this:

            __attribute__ ( ( packed ) ) 
        __attribute__ ( ( packed ) )

Now you can see that the XML file shows exactly the struct defined in the macro def_struct_data but with nice XML tags!

I hope this project could be useful, specially if you want a non-trivial example that defines a parser and grammar with lex and yacc.

8 thoughts on “Simple C to XML serializer 0.0.2 released in GitHub

  1. sc2xml-tester

    I checked sc2xml with a header file which only includes struct typedefs.
    After a while, always adding a struct, the program fails with a
    “Could not add element ***” message. It seems there are some restrictions in memory.

    1. paguilar Post author

      Hi, these messages appear when one of these libxml2 functions fail:


      Could you send me the complete logs and, if possible, the code that you are trying to convert to XML so I can identify exactly where the problem is?

      Sorry for the late reply. I just came back from holidays 🙂

  2. Hanuman

    Hi paguilar,

    I am using this tool and its great tool, while using i was observed 2 issues. Could you please clarify
    1. If header file contains C-style comments after end of line then tool throwing error
    ex: struct {
    int x; //xyaaa If i remove this commented portion working fine else error
    } A;
    2. If header file is too big, then respective xml not being generated, seems to be eventhough intput buffer contains data, but after out buffer limit exceeds then its throwing error, May i know where we need to increase the output buffer. I was tried to understand the code and found that eventhough pending data were there but if buf size is reached then we are setting YY_BUFFER_EOF_PENDING then returning. I want something that complete input parsed file should be present in xml file.

    Could you please help regarding above.

    1. paguilar Post author

      1. If you change the comment format from // that is normally used in C++, to /* */, it should work.
      2. How big is the header file in KB?
      Sorry for the late reply… it won’t happen again 🙂

  3. Hanuman


    I have a enums and typedefs in my header file, after executing sc2xml binary its not parsing the enums and typedefs, could you please let me know how to add parse code, so that my xml contains enums and typedefs.

    ex: My header contains below data:
    typedef struct {
    int a;
    } A;

    typedef enum {
    } P;

    typedef int32 XXX;

    1. paguilar Post author

      enums are not yet supported. I hope to have time to come back to this project for improving it.
      Thank you using it and all contributions are more than welcomed!!!

  4. Setu Kumar

    I’m using this tool to convert structure to xml .
    But the problem is, when there is an “include” tag in header file. It throws a syntax error while executing sc2xml command. Can you guide me on this ??
    Or, this tool only support structure in header file ?

    1. paguilar Post author

      The include token is not supported yet. As with the enums that someone else reported, I hope to have time to come back to this project for improving it.
      Of course, all contributions are more than welcomed!
      And sorry for the late reply…


Leave a Reply

Your email address will not be published. Required fields are marked *