Understanding XML

  • #1
DaveC426913
Gold Member
18,648
2,117

Main Question or Discussion Point

What's the design difference between elements and attributes? What I'm asking is: what makes you decide that something should be an element versus an attribute?

Code:
<pet>
  <name>Spike</name>
</pet>
i.e: which is better?
Code:
<pet name="Spike">
</pet>
Does the difference lie in the fact that 'Spike' is likely to be user-facing content - as opposed to some behind-the-scenes value such as a width of 200?
 
Last edited:

Answers and Replies

  • #3
DaveC426913
Gold Member
18,648
2,117
Thank you. The fact that this is a common question is good to know. And I do seem to be right on the money:

You can use either, it's a matter of design and practicality which you decide to use. But in general, the rule of thumb seems to be:
"...data goes in elements, metadata in attributes..."
 
Last edited:
  • #4
-Job-
Science Advisor
1,146
1
Generally, i prefer attributes, unless the data is large or when the data may contain some special characters, such as <, >, in which case i opt for text nodes with <![CDATA[...]]>.
 

Related Threads on Understanding XML

  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
3
Views
561
  • Last Post
Replies
1
Views
1K
Replies
4
Views
3K
Replies
3
Views
815
Top