I’m overwhelmed with attempting to design a solution for this problem – it’s a by-product of inexperience.
My goal is to read an XML input file, store the information from the XML and populate two combo boxes with data from the XML. The content of the second combo box will change based on the selection in the first.
Given this XML structure:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Category xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Node>
<ID>Unique string</ID>
<Name>Unique string</Name>
<Code>Generic string<Code>
<Kind>Generic string</Kind>
<Frame>Generic string</Frame>
...
</Node>
...
</Category>
First combo box:
Must contain only the unique values found in the the Kind section.
Second combo box:
Contains ALL of the Name entries from every Node whose Kind equals the Kind selected in the first combo box.
Regarding the XML source:
It is externally maintained and generated.
The values in the ID section are always going to be unique.
The values in the Name section are always going to be unique.
The schema will (supposedly) never change.
New unique values may appear in the Kind section in the future.
My proposed solution:
Create a class XMLNode to represent a Node from the XML source.
Members of class XMLNode correspond to the tags in each Node.
Loop through all the Nodes and create an XMLNode for each one.
While looping through nodes:
Add XMLNode objects in a hash map, with Keys = XMLNode.ID and vals = the XMLNode.
Create an array of unique Kinds.
Populate combo box one from the array of Kind entries.
Populate combo box two from the Name data for each .
Is this an appropriate approach, or have I overlooked a better/easier/more elegant solution?
If I’m on the right track, are there any obvious flaws to my proposed solution?
I ended up with a class NodeImporter and a class Node. Node represents a node, NodeImporter parses the XML. In the NodeImporter, the XML source is parsed and represented as a HashMap of HashMaps. The outer HashMap (Key,Value) is (Kind,(HashMap(Key,Value)). The inner HashMap (Key,Value) is (UinqueID,Node) for a final result of (Kind,(UniqueID,Node)). I call the final result “filteredMap”. All of NodeImporter’s fields and methods are private, except for the constructor, and a getter for filteredMap.
The class that needs the data to build the combo boxes gets filteredMap from an instance of NodeImporter. It can then get the keys of the outer HashMap to build the first ComboBoxModel. It can just as easily get the inner HashMap to be used as the HashMap for the second combo box.
pseudo code for setup:
Class NodeImporter built with class members:
Class NodeImporter constructor: