PHP - Using XML for a config file(s) are elements better than attributes or opposite?

Question!

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.



Answers

XML was invented to be a human-readable format, but it's so formalized that no one dares to edit it manually or even read it.

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



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.)
By : AlexV


This video can help you solving your question :)
By: admin