views:

98

answers:

2

I'm using XML for a config file in PHP (Using SimpleXML), in creating the config file what is more of a standard.

Having all values in elements or using the attributes?

Elements Example:

<database>
   <user>test-user</user>
   <pass>test-pass</pass>
</database>

Attribute Example:

<database user="test-user" pass="test-pass"></database>

Are there any benefits of either way?

Also other languages might need to read the same config file, such as Java and Perl.

A: 

XML were invented to be a human-readable format, but it's as much formalized as noone dares to edit it manually or even read it.

As noone would read it, I'd suggest to use PHP itself.

<?
  $config['db']['user']="user";
  $config['db']['pass']="pass";
?>  

I's slightly more secure and reliable.

Col. Shrapnel
I would like to keep it PHP as well but currently that would involve a deployment window as it would be defined as a code change. Using XML would be a config change that could be made on the fly. So no deployment window :)
Phill Pafford
Somehow, I fail to see how editing a file that contains PHP code that is executed would be more secure or more reliable than using a XML file. You can't sneak a @``$_GET[z]`` into an XML file...
Josh Davis
+3  A: 

The oldest question asked by adopters of XML is when to use elements and when to use attributes in XML design. As with most design issues, this question rarely has absolute answers, but developers have also experienced a lack of very clear guidelines to help them make this decision.

See Principles of XML design: When to use elements versus attributes by IBM. Pretty nice article.

Here's a snippet:

In my experience working with users of XML, the first question is by far the most common. In some cases the answer is pretty unambiguous:

  • If the information in question could be itself marked up with elements, put it in an element.
  • If the information is suitable for attribute form, but could end up as multiple attributes of the same name on the same element, use child elements instead.
  • If the information is required to be in a standard DTD-like attribute type such as ID, IDREF, or ENTITY, use an attribute.
  • If the information should not be normalized for white space, use elements. (XML processors normalize attributes in ways that can change the raw text of the attribute value.)
AlexV
The article looks great and many thanks. It was written in 2004 but it looks to still hold some key points.
Phill Pafford
You're welcome :)
AlexV