问题
Is there any interest to use XML over YAML for each of these types of file:
- config
- security
- routing
- services
- translations
I would like to ask XML vs YAML and JSON globally, but I'll stick to these topics :)
回答1:
In my company projects for each of mentioned above points we use YAML
because it is more readable. The most readable. The readablest.
EDIT:
The only abstract situation I can imagine for using XML
over YAML
- is probably for some dynamic file writers, since it is easier to manipulate with nodes using SimpleXML
or something like that. For example, if you need to define some configuration file, build schemas in tests etc...
But it is hard to imagine another situation.
ANOTHER EDIT:
Since my answer was accepted, I cannot disagree with m2mdas - as he mentioned in his answer below, another thing that makes sense for using XML
is IDE's autocompletion support.
回答2:
Advantage of xml congiruration is IDE auto-complete and instant validation. As elements defined by concrete schema definition, IDEs can instantly validate elements against it which is not possible in YAML or JSON. Also I think Symfony validates the xml elements in configuration against the definition before processing it.
Edit:
By validation I meant validate the element structure against the defined schema. It is better to validate the configuration before processing it. For example a services.yml
with hundreds service object definition have an error in 99th service definition. Yaml parser will parse incrementally, create expensive cpu intensive service objects and will fail at 99th definition. Whereas for service.xml
with defined schema you can validate the element structure and process them if it is OK. Obviously second process is efficient.
回答3:
Fabien Potencier wrote about the advantages of using XML over YAML on his blog:
When a XML file is loaded, it is automatically validated with the built-in services.xsd file;
The XML can be auto-completed in IDEs;
The XML format is faster than the YAML one;
The XML format as no external dependencies (the YAML format relies on the sfYAML component).
Although this post was written for symfony1.x, the points also can definitely be applied for Symfony2.
http://fabien.potencier.org/article/15/symfony-service-container-using-xml-or-yaml-to-describe-services
回答4:
Another thing in favor of XML is the possibility to define parameters using a PHP constant, which is not possible with YAML.
来源:https://stackoverflow.com/questions/11741947/symfony2-why-use-xml-for-settings-config