Trying to build hashmaps using values from XML for the purpose of sending the list of maps off through an API call to build a table in another app. I need to find the most efficient way, whether library, pattern, or even another language, to find the values in the XML and build this structure.
As of now, I am building a class for each table type. I am doing this because there are dozens of types of tables and they are all looking for different values. For example, imagine I am working for a supermarket and there is an XML document holding all the items in the store along with various details about each item. I need to build a table of items from the XML for each section in the supermarket. I would have a GroceryBuilder class, a ClothingBuilder class, and so on. So in the GrocerBuilder class, I would traverse the XML, find all the grocery items, and add the items, along with other data related to those items, to a hashmap, looking like this, where [n] equals a row:
("Grocery[1].Item", "Apple"),
("Grocery[1].Description", "Granny Smith"),
("Grocery[1].Color", "Green"),
("Grocery[2].Item", "Paper Plates"),
("Grocery[2].PricePerEach", ".03"),
("Grocery[2].Purpose", "Eating"),
("Grocery[3].Item", "Bologna"),
("Grocery[3].Description", "Meat-like"),
("Grocery[3].Purpose", "Sustainence"),
As you can see above, each row can have different column values because not every cell in the table is populated.
Here is an example of what the XML could look like:
<grocery>
<food>
<produce>
<apple>
<description>Granny Smith</description>
<itemCd>93jfu4n</itemCd>
<color>Green</color>
</apple>
<pear>
<description>Concorde</description>
<itemCd>0272ve6dg3</itemCd>
<color>Yellow</color>
</pear>
<banana>
<description>Regular</description>
<itemCd>2je7c3</itemCd>
<color>Yellow</color>
</pear>
...
<insert 50 types of produce here/>
...
</produce>
<meat>
<bologna>
<description>Meat-like</description>
<itemCd>9dmd623</itemCd>
<purpose>Sustainence</purpose>
</bologna>
...
<insert 50 types of meat here/>
...
</meat>
</food>
<sporting goods>
...
<insert 50 types of sporting goods here/>
...
</sporting goods>
<clothing>
...
<insert 50 types of clothing here/>
...
</clothing>
</grocery>
The problem I am facing is that there are potentially 100+ table types (using the example above, imagine a table for every section in the store), each looking for specific values, so I would potentially have to build 100+ different classes. I am looking for a more generic way to build these structures.
The challenge is that there are many conditions on the values I am getting from the XML. For instance, insert the value into the XML only if the ItemCd is a certain value. Or if the Apple Description equals whatever value, insert this value instead.
So far, I've been building these maps manually, looping over each item in a section (i.e. "produce"), checking conditions, and inserting the values based on those conditions. But this is going to be a ton of effort if I must do this for 100+ tables. Is there an established pattern or library that could handle this better? Or even a language other than Java?