Java 属性:.properties 文件与 xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1380156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:23:12  来源:igfitidea点击:

Java properties: .properties files vs xml?

javaxmlproperties

提问by pg-robban

I'm a newbie when it comes to properties, and I read that XML is the preferred way to store these. I noticed however, that writing a regular .properties file in the style of

我是属性的新手,我读到 XML 是存储这些属性的首选方式。但是,我注意到,以以下样式编写常规 .properties 文件

foo=bar
fu=baz

also works. This would mean a lot less typing (and maybe easier to read and more efficient as well). So what are the benefits of using an XML file?

也有效。这将意味着更少的打字(也许更容易阅读和更高效)。那么使用 XML 文件有什么好处呢?

采纳答案by Daff

In XML you can store more complex (e.g. hierarchical) data than in a properties file. So it depends on your usecase. If you just want to store a small number of direct properties a properties file is easier to handle (though the Java properties class can read XML based properties, too).

在 XML 中,您可以存储比在属性文件中更复杂(例如分层)的数据。所以这取决于你的用例。如果您只想存储少量直接属性,则属性文件更易于处理(尽管 Java 属性类也可以读取基于 XML 的属性)。

It would make sense to keep your configuration interface as generic as possible anyway, so you have no problem to switch to another representation ( e.g. by using Apache Commons Configuration) if you need to.

无论如何,保持您的配置界面尽可能通用是有意义的,因此如果需要,您可以毫无问题地切换到另一种表示形式(例如,通过使用Apache Commons Configuration)。

回答by Ben Hammond

If you have a lot of repeating data, it can be simpler to process

如果你有很多重复的数据,处理起来会更简单

<connections>
  <connection>this</connection>
  <connection>that</connection>
  <connection>the other</connection>
</connections>

than it is to process

比处理

connection1=this
connection2=that
connection3=the other

especially if you are expecting to have to store a lotof data, or it must be stored in a definite hierarchy

特别是如果您希望必须存储大量数据,或者必须将其存储在明确的层次结构中

If you are just storing a few scalar values though, I'd go for the simple Properties approach every time

如果您只是存储一些标量值,我每次都会采用简单的 Properties 方法

回答by IaCoder

XML is handy for complex data structures and or relationships. It does a decent job for having a "common language" between systems.

XML 对于复杂的数据结构和/或关系很方便。它在系统之间拥有“通用语言”方面做得不错。

However, xml comes at a cost. Its is heavy to consume. You've got to load a parser, ensure the file is in the correct format, find the information etc...

但是,xml 是有代价的。它的消耗量很大。您必须加载解析器,确保文件格式正确,查找信息等...

Whereas properties files is pretty light weight and easy to read. Works for simple key/value pairs.

而属性文件非常轻巧且易于阅读。适用于简单的键/值对。

回答by dhable

It depends on the data you're encoding. With XML, you can define a more complex representation of the configuration data in your application. Take something like the struts framework as an example. Within the framework you have a number of Action classes that can contain 1...n number of forward branches. With an XML configuration file, you can define it like:

这取决于您编码的数据。使用 XML,您可以在应用程序中定义更复杂的配置数据表示。以struts框架之类的东西为例。在该框架内,您有许多 Action 类,它们可以包含 1...n 个前向分支。使用 XML 配置文件,您可以像这样定义它:

<action class="MyActionClass">
  <forward name="prev" targetAction="..."/>
  <forward name="next" targetAction="..."/>
  <forward name="help" targetAction="..."/>
</action>

This kind of association is difficult to accomplish using just the key-value pair representation of the properties file. Most likely, you would need to come up with a delimiting character and then include all of the forward actions on a single property separated by this delimiting character. It's quite a bit of work for a hackish solution.

仅使用属性文件的键值对表示很难实现这种关联。最有可能的是,您需要想出一个分隔符,然后将所有转发操作包含在由该分隔符分隔的单个属性上。对于黑客解决方案来说,这是相当多的工作。

Yet, as you pointed out, the XML syntax can become a burden if you just want to state something very simple, like set feature blah to true.

然而,正如您所指出的,如果您只想说明一些非常简单的事情,例如将 feature blah 设置为 true,则 XML 语法可能会成为一种负担。

回答by Mike Sickler

The biggest benefit to using an XML file is that XML declares its encoding, while .properties does not.

使用 XML 文件的最大好处是 XML 声明了它的编码,而 .properties 则没有。

If you are translating these properties files to N languages, it is possible that these files could come back in N different encodings. And if you're not careful, you or someone else could irreversibly corrupt the character encodings.

如果您将这些属性文件翻译成 N 种语言,则这些文件可能会以 N 种不同的编码返回。如果您不小心,您或其他人可能会不可逆转地破坏字符编码。

回答by ilupper

If you have both hierarchical data & duplicate namespaces, then use XML.

如果您同时拥有分层数据和重复的命名空间,请使用 XML。

1) To emulate just a hierarchical structure in a properties file, simply use dot notation:

1) 要在属性文件中仅模拟层次结构,只需使用点表示法:

a.b=The Joker
a.b.c=Batgirl
a.b=Batman
a.b=Superman
a.b.c=Supergirl

So, complex (hierarchical) data representation is *not a reason to use xml.

因此,复杂(分层)数据表示*不是使用 xml 的理由。

2) For just repeating data, we can use a 3rd party library like ini4j to peg explicitly in java a count identifier on an implicit quantifier in the properties file itself.

2)对于仅重复数据,我们可以使用像 ini4j 这样的 3rd 方库在 java 中显式地将计数标识符与属性文件本身的隐式量词挂钩。

a.b=The Joker
a.b=Batgirl
a.b=Batman

is translated to (in the background)

被翻译成(在后台)

a.b1=The Joker
a.b2=Batgirl
a.b3=Batman

However, numerating same name properties still doesn't maintain the specific parent-child relationships. ie. how do we represent whether Batgirl is with The Joker or Batman?

但是,枚举同名属性仍然不维护特定的父子关系。IE。我们如何表示蝙蝠女与小丑或蝙蝠侠在一起?

So, xml is required when both features are needed. We can now decide if the 1st xml entry is what we want or the 2nd.

因此,当需要这两个功能时,需要 xml。我们现在可以决定第一个 xml 条目是我们想要的还是第二个。

[a]
   [b]Joker[/b]
   [b]
       [c]Batgirl[/c]
   [/b]
[a]

--or--

- 或者 -

[a]
   [b]Batman[/b]
   [b]
       [c]Batgirl[/c]
   [/b]
[/a]

Further detail in .... http://ilupper.blogspot.com/2010/05/xml-vs-properties.html

更多细节...... http://ilupper.blogspot.com/2010/05/xml-vs-properties.html

回答by codeforester

The disadvantages of XML:

XML 的缺点:

  1. It is hard to read - the tags make it look busier than it really is
  2. The hierarchies and tags make it hard to edit and more prone to human errors
  3. It is not possible to "append" to an XML property file to introduce a new property or provide an overriding value for an existing property so that the last one wins. The ability to append a property can be very powerful - we can implement a property management logic around this so that certain properties are "hot" and we don't need to restart the instance when these change
  1. 难以阅读 - 标签使它看起来比实际更忙
  2. 层次结构和标签难以编辑,更容易出现人为错误
  3. 不可能“附加”到 XML 属性文件以引入新属性或为现有属性提供覆盖值,以便最后一个获胜。附加属性的能力可以非常强大——我们可以围绕这个实现一个属性管理逻辑,这样某些属性是“热的”,并且当这些属性发生变化时我们不需要重新启动实例

The Java property file solves the above problems. Consistent naming conventions and dot notation can help in solving the issue of hierarchy.

Java属性文件解决了上述问题。一致的命名约定和点符号可以帮助解决层次结构问题。