2

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"
  • ItemName
    • Primary Category - "bar"
    • Secondary Category - "foo"
  • ItemName
    • Primary Category - "foo"
    • Secondary Category - "quux"
  • AnotherItem
    • Primary Category - "baz"
    • Secondary Category - "foo"
    • Tertiary Category - "monkey"
    • etc ... (no cap)
  • 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

3

Why can't you use the same structure as in your bullets?

<topLevelTag>
    <item id="1">
        <value>ItemName</value>
        <category level="primary">foo</category>
        <category level="secondary">bar</category>
    </item>
    <item id="2">
        <value>ItemName</value>
        <category level="primary">foo</category>
        <category level="secondary">bar</category>
    </item>
</topLevelTag>

(the "id" distinguishes two instances of the same itemName).

For sure, you can't have a "many to many" structure explicitly - you must pivot (root?) either on item, or on category, since you need a hierarchical data set.

Or you change completely layout and pivot on the many-to-many relationship; this is easiest to convert to and from a RDBMS storage.

<topLevelTag>
    <item id="1">...</item>
    <category id="1">foo</category>
    <mapping>
        <map item="1" category="1" />
        <map item="1" category="2" />
        <map item="2" category="2" />
    </mapping>
</topLevelTag>
  • Can you explain and/or provide a link as to what you mean by "pivot"? – durron597 Mar 24 '15 at 13:55
  • I am sorry, I am translating - English is not my native language, and I sometimes make up words. What I mean is that one type of node ("item") must be above and have "category" below, or vice versa; in the first case I would say that the structure is... hinged? on items, in the other, on categories. In the last example I'm using the term improperly, as all three entities are instances of something which is no longer a topLevelTag, but perhaps a collection, or a table. – LSerni Mar 24 '15 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.