tags:

views:

29

answers:

1

i have a database that contains countries and cities. i want to export this information to a xml document, but wonder how to structure it.

should i do it like this:

<country>
 <name>Canada</name>
 <city>
  <name>Toronto</name>
  <population>1423200</population>
 </city>
 <city>
  <name>Ottawa</name>
  <population>1423200</population>
 </city>
</country>

or like this:

<country>
  <name>Canada</name>
  <cities>
     <city>
      <name>Toronto</name>
      <population>1423200</population>
    </city>
    <city>
      <name>Ottawa</name>
      <population>1423200</population>
    </city>
  </cities>
</country>

or like this:

<entity>
 <country>Canada</country>
 <city>Ottawa</city>
 <city_population>1423200</city_population>
</entity>
<entity>
 <country>Canada</country>
 <city>Toronto</city>
 <city_population>1423200</city_population>
</entity>

what are the pros and cons with each of them? is there another way of structuring?

which of them is best for future changes (adding data).

my first time to structure in xml, so would be great with feedbacks/hints!

+3  A: 

You should structure you XML Documents in the same manner you would structure you class in the code. So as cities population is property of the city itself - it should be child of the city node. I would go for the 2nd structure.

Plus it's more mnemonic about your objects. For example it's not clear what 'entities' stays for in your second solution.

Plus it has less data repetition, as you have to state the country=canada in every entity. I would make a change to you first solution, though. Put the Country element in a collection:

<countries>
<country>
 <name>Canada</name>
 <cities>
 <city>
  <name>Toronto</name>
  <population>1423200</population>
 </city>
 <city>
  <name>Ottawa</name>
  <population>1423200</population>
 </city>
 </cities>
</country>
</countries>

It will help you extend later your data.

EDIT: In general, when you have a repetition of objects it's better to wrap them in a 'collection' element. It's a good practice since you can add properties into the collection itself and some other benefits - you won't have iterate in to the parents' elements and choose which one belongs to the same type.

anthares
so u mean the first one?
weng
Definitely the first one is better!
anthares
ok thanks for the change. but i will have one country in each file so that won't be necessary in this case.
weng
You will never know :)
anthares
yeah:) i have made a change in my question. i added another solution, could you have a look at the middle one and share your thoughts. is that a better structure?
weng
middle is better now :) You can use the built-in serializers and deserializers to manipulate your XML Document. It will have hard time if you don't wrap your collection-items in a collection.
anthares
not sure i understood u...built-in serializers and deserializers where? in php? what are the function names? and what do you mean with the collection part?
weng
Yeah .. php for example (or what ever you write your code to). These are object that transform your xml text to object and vice-verse. Read here http://www.devshed.com/c/a/PHP/Serializing-XML-With-PHP/It's easier to parse the XML Document if the collections are wrapped. I mean that parser will have hard times when dealing with your first structure. He should be aware that all 'city' elements should go. It's not impossible, but it's easier when they are wrapped in 'cities' element.
anthares
ah ok i understand u now=) thanks!
weng