Re: [xml] Best way to do C Struct <-> XML Node tree?



Vedantam, Pavan <Pavan Vedantam fmcti com> wrote:
Hi Tyler,

I implemented something similar to what you are doing, this is what I
do, "read the contents of an XML file directly to a C Struct, and also
write back to the XML file with the values from the C struct", I have an
XML file which is similar to the one below,

<?xml version="1.0" encoding="UTF-8" ?> 
 <!--  Sample template of the XML file to be used --> 
<XML_TO_C>
  <bb type="string">XML is the in thing !!</bb> 
  <ex2 type="array" size="6">10,20,30,786,50,80</ex2> 
</XML_TO_C>

Hope this helps,

        Interesting approach! I was thinking of something schema-independant
though. I've got it working now, there's just one aggrivating bit of C code
that i have to do to translate between printf formatting codes and C
datatypes (you'd figure something like this would be part of the libc
core...)

#define DATA2XML(s,t) if(!strcmp(description.format, s)) { sprintf((char*)__buffer, description.format, 
*((t*)data)); }

xmlNodePtr bt_data2xml(xmlNodePtr parent, bt_xml_node description, void*
data) {
    if(!strcmp(description.format, "%s")) {
        strcpy((char*)__buffer, (char*)data);
    }
    else DATA2XML("%llu", u_int64_t)
    else DATA2XML("%lu", time_t)
    else DATA2XML("%u", u_int32_t)
    else {
        __buffer[0] = 0;
        fprintf(stderr, "Warning: %s is null: unknown format '%s'\n",
description.name, description.format);
    }
    
    return xmlNewTextChild(parent, NULL, description.name, __buffer);    
}

        Maybe one day I'll figure out a way to get rid of that, but it works
for now. :-)

                - Tyler




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]