views:

63

answers:

4

Example XML using element nodes:

<?xml version="1.0" encoding="utf-8"?>
<users>
  <user>
    <name>David Smith</name>
    <phone>0441 234443</phone>
    <email>[email protected]</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="[email protected]">
    <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?

A: 

I would prefer using attributes over sub-element.

ydobonmai
Would you care to explain ? I'd be interested to know your reasons, I guess OP would be too.
High Performance Mark
A: 

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
+2  A: 

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
+1  A: 

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.
xqib-team