views:

67

answers:

1

i wonder how i could store a xml structure in a persistence layer.

cause the relational data looks like:

<entity id="1000070">
    <name>apple</name>
    <entities>
        <entity id="7002870">
            <name>mac</name>
            <entities>
                <entity id="7002907">
                    <name>leopard</name>
                    <entities>
                        <entity id="7024080">
                            <name>safari</name>
                        </entity>
                        <entity id="7024701">
                            <name>finder</name>
                        </entity>
                    </entities>
                </entity>
            </entities>
        </entity>
        <entity id="7024080">
            <name>iphone</name>
            <entities>
                <entity id="7024080">
                    <name>3g</name>
                </entity>
                <entity id="7024701">
                    <name>3gs</name>
                </entity>
            </entities>
        </entity>
        <entity id="7024080">
            <name>ipad</name>
        </entity>
    </entities>
</entity>

as you can see, it has no static structure but a dynamical one. mac got 2 descendant levels while iphone got 1 and ipad got 0.

i wonder how i could store this data the best way? what are my options. cause it seems impossible to store it in a mysql database due to this dynamical structure.

is the only way to store it as a xml file then? is the speed of getting information (xpath/xquery/simplexml) from a xml file worse or greater than from mysql?

what are the pros and cons? do i have other options? is storing information in xml files, suited for a lot of users accessing it at the same time?

would be great with feedbacks!! thanks!

EDIT: now i noticed that i could use something called xml database to store xml data. could someone shed a light on this issue? cause apparently its not as simple as just store data in a xml file?

+1  A: 

entity has a 0..1 relation to entities, and entities has a 0..* relation to entity.

This is not the exact SQL, and most likely not valid in any DBMS, but should help you get started:

CREATE TABLE entity (
  id int(10) AUTOINCREMENT NOT NULL,
  xid int(10) NOT NULL,
  name char(30) NOT NULL,
  entities_id int(10) REFERENCES entities.id NULL,
  PRIMARY KEY(id)
)

CREATE TABLE entities (
  id int(10) AUTOINCREMENT NOT NULL,
  entityref_id int(10) REFERENCES entityref.id NOT NULL,
  PRIMARY KEY(id)
)

CREATE TABLE entityref (
  id int(10) NOT NULL,
  entity_id int(10) REFERENCES entity.id NOT NULL,
  PRIMARY KEY(id,entity_id)
)
Ignacio Vazquez-Abrams
but this seems very confusing to convert xml to sql database? and as u said..its not valid? isnt it better to store it as xml so you dont have to have a middle man? what if eg. one element got 1000 descendant levels..would be so "messy" to navigate in a sql database for the relations.
never_had_a_name
The structure is valid. The SQL is not. http://stackoverflow.com/questions/2260303/storing-xml-in-a-database-for-flexible-content/2260323#2260323
Ignacio Vazquez-Abrams
i have one question about the entities table. isnt it better if you in the table 'entities' only have one 'id' thus deleting 'entityref_id'. cause the 'id' could refer to the 'entityref_id' instead. why have two ids in 'entities'?
never_had_a_name
Your `id` attributes in the XML sample you gave are not unique. If the actual values *are* unique then yes, you can collapse it into one field.
Ignacio Vazquez-Abrams
no im not talking about the table 'entity'. im talking about the table 'entities'. there are 2 ids: 'id' and 'entityref_id'. why should i have two ids? they should be the same and refer to 'id' in table 'entityref'. isnt that better? i dont get the purpose of the 'entityref_id' in table 'entities'.
never_had_a_name
Ah, I see now. Since the `entities` tag has neither attributes nor any other children it is possible to collapse them into a single table.
Ignacio Vazquez-Abrams
sorry i dont understand, what do u mean by nor any other children? they got 'entity' as children. and could u explain "collapse them into a single table"?
never_had_a_name
also i have noticed that you have created a circular constraint issue. cause u cant create a new row in entity, entities or entityref cause in each table there is one constraint to another. so its impossible to create a new row. at least one table has to have NO constraint. hmmm..any suggestions?=)
never_had_a_name
*Other* children. As in only `entity`. "Collapse into a single table" = "make a single table provide all functionality". `entity.entities_id` is set `NULL`, therefore it is possible to create an entry in `entity` without a reference to another entry.
Ignacio Vazquez-Abrams
so you mean i should only have 2 tables: entity and entityref where entity.entities_id is refering to entityref.id?
never_had_a_name
Yes, that would work.
Ignacio Vazquez-Abrams
hmm..i have discovered that i could have only ONE table. in that table i have id (primary key), entity_id (eg 1231231) and parent_id (what id or entity_id it belongs to). thats it? seems very simple and useful. why should i even have 2 or 3 tables:) what do you think?
never_had_a_name
That would work for storing it. Generating XML from it would be a bit trickier, but still possible.
Ignacio Vazquez-Abrams
i dont see the difference why it should be harder than the 2-3 tables structure? you are so fast in responding..you got mail for every reply?=)
never_had_a_name