David Smith ...">

XML attributes or element nodes?

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 need to create an XML file based on data from a relational database and cannot decide whether to use attributes or elements.

What is the best practice when creating XML files and why?

+4
source share
5 answers

One of the best articles I've read is XML Design Principles: When to Use Elements Against Attributes, "that don't try to give you a direct answer, but bring good food for thought along with examples.

I would suggest that the advice in the article is based on your first design; You may be interested in reading the part on the representation of names.

+5
source

To help you of your choice, you need to know 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 cannot assign two phone attributes to the user.
  • In an element, you can nest additional child elements, for example, with addresses. For example, you can enclose first name, first name, and last name in the username. You cannot do this with an attribute, so attributes will only work for a relatively flat hierarchy.
  • If you use namespaces, children inherit the default namespace binding. There are no attributes.
  • The elements of the child are ordered, there are no attributes: if you use attributes, you cannot rely on the stored order.
  • In the element, you have the option of using the CDATA section, so you don’t have to worry about escaping special characters like <. In the attribute, you will either have to bypass all quotation marks with "if the attribute has a double quote or to avoid all apostrophes with & apos; if the attribute value is in single quotes.
+3
source

I would prefer 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 select items for things that can be repeated and can have subelements, and in other cases I would personally use attributes because they can save a lot of memory space. As the xdib command said:

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

This is why I prefer the syntax above. I decided to take an element for the name, but I have enough parameters for the last name as an attribute, and I think that in this case there are enough attributes, because nothing else should be nested under them. You can have multiple addresses, but each address can have one street and one city. This is how I choose.

+1
source

I use the attribute only for identifiers (in this case I would put only the country in the attribute), when you have text, this is better than the + CDATA element (if you are sure that you will never have a special char you can omit cdata in your text) .

0
source

I would rather use attributes over a subitem.

-2
source

Source: https://habr.com/ru/post/1304446/


All Articles