Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent a Many-To-Many relationship in XML or other simple file format?

I have a list management appliaction that stores its data in a many-to-many relationship database.

I.E. A note can be in any number of lists, and a list can have any number of notes.

I also can export this data to and XML file and import it in another instance of my app for sharing lists between users. However, this is based on a legacy system where the list to note relationship was one-to-many (ideal for XML).

Now a note that is in multiple lists is esentially split into two identical rows in the DB and all relation between them is lost.

Question: How can I represent this many-to-many relationship in a simple, standard file format? (Preferably XML to maintain backwards compatibility)

like image 305
CodeFusionMobile Avatar asked Jun 17 '10 22:06

CodeFusionMobile


People also ask

How do you create a many to many relationship in DBMS?

Connect the three tables to create the many-to-many relationship. To complete the many-to-many relationship, create a one-to-many relationship between the primary key field in each table and the matching field in the intermediate table.

How is represented in XML?

A relational database consists of a set of tables, where each table is a set of records. A record in turn is a set of fields and each field is a pair field-name/field-value. All records in a particular table have the same number of fields with the same field-names.

What are the formats in which XML data is stored?

XML stores data in plain text format. This provides a software- and hardware-independent way of storing, transporting, and sharing data. XML also makes it easier to expand or upgrade to new operating systems, new applications, or new browsers, without losing data.

What is XML data representation?

Abstract. EXtensible Markup Language (XML) technology provides an ideal representation for the complex structure of models and neuroscience data, as it is an open file format and provides a language-independent method for storing arbitrarily complex structured information.


1 Answers

I think your XML format must make use of some sort of "references". If you prefer the list-to-notes relationship to be visible, then something like the following can be suitable:

<notes>
    <note id="Note1"> ... </note>
    <note id="Note2"> ... </note>
    ...
</notes>

<lists>
    <list id="List1">
        <note_refs>
            <note_ref id="Note1"/>
            <note_ref id="Note4"/>
        </note_refs>
    </list>
    ...
</lists>

If on the other hand you want to see easily the lists associated with a given note, then you can simply invert the roles of lists and notes in my example.

Alternatively, you can represent the many-to-many relationship more symmetrically as a mathematical relation: define all notes, define all lists, and then define a mapping consisting of [list reference, note reference] pairs.

like image 166
Eyal Schneider Avatar answered Oct 01 '22 00:10

Eyal Schneider