我有一个包含国家和城市的数据库。 我想将此信息导出到xml文档,但想知道如何构造它。
我应该这样做:
<country> <name>Canada</name> <city> <name>Toronto</name> <population>1423200</population> </city> <city> <name>Ottawa</name> <population>1423200</population> </city> </country>或者像这样:
<country> <name>Canada</name> <cities> <city> <name>Toronto</name> <population>1423200</population> </city> <city> <name>Ottawa</name> <population>1423200</population> </city> </cities> </country>或者像这样:
<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>他们每个人的利弊是什么? 还有另一种结构方式吗?
其中哪一个最适合未来的变化(添加数据)。
我第一次在xml中构建,所以反馈/提示会很棒!
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!
最满意答案
您应该以与在代码中构造类相同的方式构建XML文档。 因此,城市人口是城市本身的财产 - 它应该是城市节点的孩子。 我会去第二个结构。
另外,它对你的物体更为谨慎。 例如,在第二个解决方案中,不清楚“实体”的用途。
此外,它具有较少的数据重复,因为您必须在每个实体中声明country = canada。 不过,我会改变你的第一个解决方案。 将Country元素放在集合中:
<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>它可以帮助您延长数据延迟。
编辑:通常,当您重复对象时,最好将它们包装在“集合”元素中。 这是一个很好的做法,因为您可以将属性添加到集合本身以及其他一些好处 - 您将不会迭代到父项元素并选择属于同一类型的元素。
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.
更多推荐
发布评论