PHP - 对配置文件使用 XML 是元素比属性更好还是相反?
我在 PHP 中使用 XML 作为配置文件(使用 SimpleXML),在创建配置文件时更像是一个标准。
拥有元素中的所有值还是使用属性?
元素示例:
<database>
<user>test-user</user>
<pass>test-pass</pass>
</database>
属性示例:
<database user="test-user" pass="test-pass"></database>
这两种方式都有什么好处吗?
其他语言也可能需要读取相同的配置文件,例如 Java 和 Perl。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅XML 设计原则:何时使用元素与属性由 IBM 提供。相当不错的文章。
以下是一个片段:
根据我与 XML 用户合作的经验,第一个问题是迄今为止最常见的。在某些情况下,答案非常明确:
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:
XML 被发明为一种人类可读的格式,但它过于形式化,以至于没有人敢手动编辑它,甚至没有人敢阅读它。
由于没有人会读它,我建议使用 PHP 本身。
它稍微更安全和可靠。
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.
It's slightly more secure and reliable.