Attributes vs. Values

J

jason.cipriani

I'm just recently starting to use XML for storing data, and I am
wondering when it is more appropriate to use child node values instead
of node attributes -- and in general, what are good ways to structure
the XML. For example, let's say I want to store information about the
furniture in a room. This room has a red, wooden chair. It has a
green ceramic lamp. It also has a white end table made of, I don't
know, let's say human bone (scary!). So one way I could say this in
XML is:

<furniture>
<chair>
<color>red</color>
<material>wood</material>
</chair>
<lamp>
<color>green</color>
<material>ceramic</color>
</lamp>
<table>
<type>endtable</type>
<color>white</color>
<material>bone</material>
</table>
</furniture>

Ok... but I could also do, say:

<item what="chair">
...
</item>
<item what="lamp">
...
</item>
<item what="table">
...
</item>

Or for that table, I can remove "type" child node and make that an
attribute, maybe that is more appropriate:

<item what="table" subtype="endtable">...</item>

Or I could just use attributes for everything (this one seems to be
the most natural to me...):

<furniture>
<chair color="red" material="wood"/>
<lamp color="green" material="ceramic"/>
<table color="white" material="bone" type="end"/>
</furniture>

Or I could combine the above two:

<furniture>
<item what="chair" color="red" material="wood"/>
<item what="lamp" color="green" material="ceramic"/>
<item what="table" color="white" material="bone" type="end"/>
</furniture>

I could even do this:

<item what="chair">
<color value="red"/>
<material value="ceramic"/>
</item>

Or this:

<item>
<property name="what" value="chair"/>
<property name="color" value="red"/>
<property name="material" value="ceramic"/>
</item>

Or this:

<item>
<property name="what">chair</property>
<property name="color">red</property>
<property name="material">ceramic</property>
</item>

Or any number of the infinite other possibilities I can think of...

When does it make sense to use values vs. attributes? When does it
make sense to have different child node types (like "chair", "lamp",
"table") rather than the same child node types but different
attributes (all child nodes are "item" but they have a "what"
attribute)?

It's really confusing because I have so many options.

Thanks,
Jason
 
J

jason.cipriani

There is an article on this in the XML FAQ:http://xml.silmaril.ie/developers/attributes/

Thanks for that! That's exactly the kind of info I was looking for.

"Beginners always ask this question. Those with a little experience
express their opinions passionately. Experts tell you there is no
right answer."

Makes me feel a little more secure about asking this question; and
reading through everything else makes me feel a lot better about just
picking whatever is easier in the situation.

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top