Reputation: 123
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?
Upvotes: 2
Views: 459
Reputation: 12075
so I would potentially have to build 100+ different classes I am looking for a more generic way to build the
and maybe the grocery may introduce a new product type anytime (assumption)
In this case you right and defining a class for each type is not effective. You had good intuition to ask for a more generic approach.
I don't see other requirements or constraints so it is hard to tell what is exactly a good solution.
For start I'd propose to have a common parametrized TypeBuilder - a parser or filter (e. g. pass the required type name as a parameter returning a set of any properties as a map from the parsed input? A parser returning a list of tbe TypeBuilder instances? )
The challenge is that there are many conditions on the values I am getting from the XML.
Your intution is right again, it is considered as bad practice to put (business) rules into the code.
If you cannot find some generic set of rules, then you may need to put the rules somewhere. Maybe a rule engine is an overkill (feasible for enterprises, such as supermarkets), maybe a list of regular expressions could be good enough. Without more requirements or constraints it is hard to propose better answer
Upvotes: 0
Reputation: 111491
To select parts of an XML document use XPath; to transform from one XML document to another, or even construct an XML document from text or JSON, use XSLT (which embeds XPath). XPath supports the powerful conditionality that you describe and more.
See also
There are data binding tools such as Jakarta XML Binding that can help automate the mapping between XML and Java objects.
See also
Upvotes: 1