XML: When to use attributes instead of child nodes?
Posted
by Rosarch
on Stack Overflow
See other posts from Stack Overflow
or by Rosarch
Published on 2010-03-15T01:37:12Z
Indexed on
2010/03/15
1:39 UTC
Read the original article
Hit count: 340
Xml
|coding-style
For tree leaves in XML, when is it better to use attributes, and when is it better to use descendant nodes?
For example, in the following XML document:
<?xml version="1.0" encoding="utf-8" ?>
<savedGame>
<links>
<link rootTagName="zombies" packageName="zombie" />
<link rootTagName="ghosts" packageName="ghost" />
<link rootTagName="players" packageName="player" />
<link rootTagName="trees" packageName="tree" />
</links>
<locations>
<zombies>
<zombie>
<positionX>41</positionX>
<positionY>100</positionY>
</zombie>
<zombie>
<positionX>55</positionX>
<positionY>56</positionY>
</zombie>
</zombies>
<ghosts>
<ghost>
<positionX>11</positionX>
<positionY>90</positionY>
</ghost>
</ghosts>
</locations>
</savedGame>
The <link>
tag has attributes, but it could also be written as:
<link>
<rootTagName>trees</rootTagName>
<packageName>tree</packageName>
</link>
Similarly, the location tags could be written as:
<zombie positionX="55" positionY="56" />
instead of:
<zombie>
<positionX>55</positionX>
<positionY>56</positionY>
</zombie>
What reasons are there to prefer one over the other? Is it just a stylistic issue? Any performance considerations?
© Stack Overflow or respective owner