views:

43

answers:

3

This is not a technical question. More of a standardizing thing. I have a bunch of business objects that I need to serialize to XML. In its simplest form, I got object X holding objects of type Y. Y is extended to Y1, Y2, Y3.

My question is, should I go with:

<X>
  <Y>
    <Y1 />
    <Y2 />
    <Y1 />
    <Y3 />
  </Y>
</X>

or:

<X>
  <Y Type="Y1" />
  <Y Type="Y2" />
</X>

Of course, first way is more readable, which is what I'm thinking of going for. Any reason why this isn't good? I guess I'm after opinion... ;)

A: 

Well i think it depends on how you are are using the serialized data - is it for interchange, temp storage, long term storage, etc.. More importantly do you need to create instances of the objects from the serialization?

If its for interchange then I would say its the property names themselves and any values they hold that are important not so much the type thus the type should be an attribute. On the other hand, if this is for storage purposes then the type names might be just as important if you need to instantiate the object based on the serialization and that might make using the type names as elements better.

prodigitalson
+1  A: 

I don't think there is answer to such a generic question -- you really need to give more concrete example as to what you are trying to achieve. Format does not exist in vacuum; expected usage really matter ("form follows function") here too.

StaxMan
+1  A: 

I try to explain in other words what other people already wrote to you. Your question contain too less information to be answered. Why?

In my opinion the main problem in your question is that you give us too less information about the subject where you want use XML. XML is a metalanguage and not a language. XML defines only some simple basis rules. For example XHTML, XML Schema or XQuery are XML-based languages. How we can compare there?. It's much more easy to give you recommendation about the usage of some language construction of one language as to give you any recommendation in a very general case.

One can give you general restrictions which have XML elements and which have XML attributes. Some good links you can find in the comment of jasso to your question. But the information along will not solve your problem.

The answer like "elements are seems better in your case" solve your problem also not really. For example, you can write the information, that "Y is extended to Y1, Y2, Y3" in different ways. For example like

<Y1 base="Y" />
<Y2 base="Y" />
<Y3 base="Y" />

or

<class name="Y1" base="Y" />
<class name="Y2" base="Y" />
<class name="Y3" base="Y" />

or some other ways and not like you wrote in your question. All these XML documents are different XML languages. The problem is not only the information which you have, but also from which side you want look at the information. How we can compare different "XML languages" which can contain the same information? If you write some information in XML file you want probably to read the information sometime later. Depend on what information you need later and in which situation, in which order and so on are required to compare different suggestion XML languages which you can use.

So a general question about the metalanguage XML can not be good answered.

Oleg