I have an xml file format that looks something like this:
<topLevelTag>
<category name="foo">
<item attrib="value">ItemName</item>
<item attrib="value2">AnotherItem</item>
</category>
<category name="bar">
<item attrib="value">ItemName</item>
<item attrib="value2">AnotherItem</item>
</category>
</topLevelTag>
The code that parses this creates an instance of ItemName
and is told that it's one category is, in the first case, "foo"
, and then creates a second instance of ItemName
which has associated category "bar"
.
The problem is that I need to redesign the system so that each Item
can have more than one Category.
But I still need to be able to create multiple items as well. For example (using bullets instead of xml) I might need to create the following instances:
ItemName
- Primary Category -
"foo"
- Secondary Category -
"bar"
- Primary Category -
ItemName
- Primary Category -
"bar"
- Secondary Category -
"foo"
- Primary Category -
ItemName
- Primary Category -
"foo"
- Secondary Category -
"quux"
- Primary Category -
AnotherItem
- Primary Category -
"baz"
- Secondary Category -
"foo"
- Tertiary Category -
"monkey"
- etc ... (no cap)
- Primary Category -
StillAnotherItem
- etc ...
- etc ... (no cap)
How can I design my XML format to encapsulate this many-to-many relationship?
I am aware this question is on the borderline between StackOverflow and Programmers. I chose to put it here because it is a software architecture and design question. If not please let me know and I'll be happy to move it. Precedent one | Precedent two | Precedent three