4

Example XML using element nodes:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <name>David Smith</name>
    <phone>0441 234443</phone>
    <email>dave.s33@domain.com</email>
    <addresses>
      <address>
        <street>1 Some Street</street>
        <town>Toy Town</town>
        <country>UK</country>
      </address>
      <address>
        <street>5 New Street</street>
        <town>Lego City</town>
        <country>US</country>
      </address>
    </addresses>
  </user>
</users>

Example XML using attributes:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user name="David Smith" phone="0441 234443" email="dave.s33@domain.com">
    <addresses>
      <address street="1 Some Street" town="Toy Town" country="UK" />
      <address street="5 New Street" town="Lego City" country="US" />
    </addresses>
  </user>
</users>

I'm needing to build an XML file based on data from a relational database and can't work out whether I should use attributes or elements.

What is best practice when building XML files and why?

Camsoft
  • 11,718
  • 19
  • 83
  • 120
  • 1
    There are several duplicates of this same question on SO, including one from within the last week. – Rob Mar 18 '10 at 11:12

5 Answers5

5

One of the better articles I've read is "Principles of XML design: When to use elements versus attributes", which doesn't attempt to give you an outright answer but does give some good food for thought along with examples.

I would suggest that the advice in the article leans toward your first design; you may be interested to read the part about representing names though.

Greg Beech
  • 133,383
  • 43
  • 204
  • 250
3

To help you in your choice, you need to be aware of the following differences between elements and attributes:

  • An element may not have two attributes with the same name. In particular, if you use attributes, you can not assign two phone attributes to a user.
  • In an element, you can nest further child elements, like you did with addresses. For example, you can nest a first-name, a middle-name and a last-name element in the name of a user. With an attribute, you cannot do this, so that attributes will only work for a relatively flat hierarchy.
  • If you are using namespaces, child elements inherit the default namespace binding. Attributes do not.
  • Child elements are ordered, attributes are not: if you use attributes, you cannot rely on their order being preserved.
  • In an element, you have the possibility to use a CDATA section so that you need not worry about escaping special characters like <. In an attribute, you will have to either escape all quotes with &quot; if the attribute is double-quoted, or to escape all apostrophes with &apos; if the attribute value is in single quotes.
Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
1

I would preffer the following syntax:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <name first-name="David" last-name="Smith" /> 
    <phone home-phone="0441 234443" />
    <email private-email="dave.s33@domain.com" />
    <addresses>
      <address street="1 Some Street" town="Toy Town" country="UK" />
      <address street="5 New Street" town="Lego City" country="US" />
    </addresses>
  </user>
</users>

I think it looks better if you choose elements for stuff that can be repeated and can have sub-elements and in other cases i personally would use attributes, because they can save a lot of memory space. As xdib-team said above:

An element may not have two attributes with the same name. In particular, if you use attributes, you can not assign two phone attributes to a user.

That is why i preffer the syntax above. I've decided to take element for a name but i have enough options for the surname as attribute and i think attributes are enough in this case because there is nothing more that should be nested beneath them. You can have more than one address but each address can have one street and one town. That is the way how i choose.

orustammanapov
  • 1,792
  • 5
  • 25
  • 44
0

I use attribute only for IDs (in this case I'd put only country into an attribute), when you have some text it's better element + CDATA (if you're sure you'll never have special char in your text you can omit the cdata).

remi bourgarel
  • 9,231
  • 4
  • 40
  • 73
-2

I would prefer using attributes over sub-element.

Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134