I have data which I store in XML files and manipulate in my program. This data is destined for EEPROM/FLASH of a hardware device and needs to be formatted into a binary table of sorts before it can be loaded into the device. This table is of variable length but the fields in each row are to be fixed. I would like the flexibility of saving this data as different binary table versions/formats so I thought it would be slick to define these different formats in a seperate XML file.
Example:
I have the following XML which describes a table of users. Each user would then represent a row with the attributes of that user being fields in a row.
<?xml version="1.0"?>
<libraries>
<library>
<users>
<user name="John" id="0" email="john@john.com" cellphone="555-111-1111"/>
<user name="Jason" id="1" email="jason@jason.com" cellphone="555-111-1112"/>
<user name="Jann" id="7" email="jann@jann.com" cellphone="555-111-1113"/>
</users>
</library>
</libraries>
I also have the following XML which describes a transformation of the data above. Notice there could be different versions of structure (only version 1 in this case) so that there are multiple 'save as' options.
<?xml version="1.0"?>
<dataStructures>
<structure version="1">
<table name="users" order="2">
<field name="id" nullable="false" length="4" order="1" type="int" max="255"/>
<field name="name" nullable="false" length="32" order="2" type="string"/>
<field name="email" nullable="false" length="32" order="3" type="string"/>
<field name="cellphone" nullable="false" length="32" order="4" type="string"/>
</table>
</structure>
</dataStructures>
Structure version 1 shows that a table based on the users data is to be constructed and it is to be the second table in the list (order=2). It then goes on to call out each field in the table, if null is acceptable, it's length in bytes, the order in the row, and the type of data.
You get the idea... hopefully. :)
So it's a neat idea to me, but implementing it is the problem. Say my data was deserialized into a list Users. Would it be best to make a class called DataTransformation, deserialize the structure into it, then have a method of the DataTransformation class which operates on this list based on the information obtained from the structure XML? Any hints as to how I might proceed? Any recommendations to a more efficient method?

1 answers
Well, you could do it like that but it's going to be complex to code as it's very general.
If the fields of the User class (or any other class) are always going to be formatted into a row of the binary table in the same order and at the same offsets, it'll be much easier to just code a separate method for each such class.
answered 5 months ago by:
11603