Reputation: 85036
I prefer to have my XML documents structured in the following way:
<root>
<node info1="blah" info2="blah" />
<node info1="blah" info2="blah" />
</root>
Instead of:
<root>
<node>
<info1>blah</info1>
<info2>blah</info2>
<node>
<node>
<info1>blah</info1>
<info2>blah</info2>
<node>
</root>
I think this makes things easier to read and makes navigation simpler. Are there any reasons that the second example would be better? Is it same to assume my structure is better?
Obviously if there is a one to many relationship in the data I have no problem moving it to it's own child node.
Upvotes: 2
Views: 78
Reputation: 5333
Two yet unmentioned differences:
Upvotes: 0
Reputation: 602
I like the attributes in the interest of brevity and expressiveness.
I would go with the sub node in the following cases:
Sometimes, in my configuration files I support both syntaxes so that the author of the configuration file can make the decision to use one of them as they deem appropriate.
Upvotes: 2
Reputation: 98459
The two types are slightly different in terms of the constraints that may be imposed by a DTD, and in terms of ordering for streaming (all attributes of a node are delivered before any of its child nodes).
Otherwise, you're right that they're interchangeable and the attribute syntax is more concise.
Upvotes: 0