PHP - 对配置文件使用 XML 是元素比属性更好还是相反?

发布于 2024-08-23 16:04:00 字数 408 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朕就是辣么酷 2024-08-30 16:04:00

采用者提出的最古老的问题
XML 的关键是何时使用元素和
何时在 XML 设计中使用属性。
与大多数设计问题一样,这
问题很少有绝对的答案,
但开发商也经历了
缺乏非常明确的指导方针来提供帮助
他们做出这个决定。

请参阅XML 设计原则:何时使用元素与属性由 IBM 提供。相当不错的文章。

以下是一个片段:

根据我与 XML 用户合作的经验,第一个问题是迄今为止最常见的。在某些情况下,答案非常明确:

  • 如果相关信息本身可以用元素标记,则将其放入元素中。
  • 如果信息适合属性形式,但最终可能会在同一元素上成为同名的多个属性,请改用子元素。
  • 如果信息需要采用类似 DTD 的标准属性类型(例如 ID、IDREF 或 ENTITY),请使用属性。
  • 如果信息不应针对空白进行规范化,请使用元素。 (XML 处理器以可以更改属性值的原始文本的方式规范属性。)

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.)
獨角戲 2024-08-30 16:04:00

XML 被发明为一种人类可读的格式,但它过于形式化,以至于没有人敢手动编辑它,甚至没有人敢阅读它。

由于没有人会读它,我建议使用 PHP 本身。

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

它稍微更安全和可靠。

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.

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

It's slightly more secure and reliable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文