简体   繁体   English

Java属性:.properties文件vs xml?

[英]Java properties: .properties files vs xml?

I'm a newbie when it comes to properties, and I read that XML is the preferred way to store these. 在涉及属性时,我是新手,我读到XML是存储这些的首选方式。 I noticed however, that writing a regular .properties file in the style of 然而,我注意到,编写一个常规.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文件有什么好处?

In XML you can store more complex (eg hierarchical) data than in a properties file. 在XML中,您可以存储比属性文件中更复杂(例如,分层)的数据。 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). 如果您只想存储少量直接属性,则属性文件更易于处理(尽管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 ( eg by using Apache Commons Configuration ) if you need to. 无论如何,保持配置界面尽可能通用是有意义的,因此如果需要,可以切换到另一种表示(例如,通过使用Apache Commons Configuration )。

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. 如果要将这些属性文件转换为N种语言,则这些文件可能会以N种不同的编码形式返回。 And if you're not careful, you or someone else could irreversibly corrupt the character encodings. 如果你不小心,你或其他人可能会不可逆转地破坏角色编码。

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 lot of 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 如果你只是存储一些标量值,我每次都会选择简单的属性方法

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这样的第三方库在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? 我们如何表示Batgirl是与The Joker还是Batman?

So, xml is required when both features are needed. 因此,当需要这两个功能时,需要xml。 We can now decide if the 1st xml entry is what we want or the 2nd. 我们现在可以决定第一个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://upupper.blogspot.com/2010/05/xml-vs-properties.html

XML is handy for complex data structures and or relationships. XML对于复杂的数据结构和/或关系非常方便。 It does a decent job for having a "common language" between systems. 它在系统之间拥有“共同语言”的工作做得不错。

However, xml comes at a cost. 但是,xml需要付出代价。 Its is heavy to consume. 它的消耗量很大。 You've got to load a parser, ensure the file is in the correct format, find the information etc... 你必须加载解析器,确保文件格式正确,找到信息等...

Whereas properties files is pretty light weight and easy to read. 而属性文件非常轻,易于阅读。 Works for simple key/value pairs. 适用于简单的键/值对。

It depends on the data you're encoding. 这取决于您编码的数据。 With XML, you can define a more complex representation of the configuration data in your application. 使用XML,您可以在应用程序中定义更复杂的配置数据表示。 Take something like the struts framework as an example. 以struts框架为例。 Within the framework you have a number of Action classes that can contain 1...n number of forward branches. 在框架内,您有许多Action类,可以包含1 ... n个前向分支。 With an XML configuration file, you can define it like: 使用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. 对于一个hackish解决方案来说,这是相当多的工作。

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. 然而,正如您所指出的,如果您只想表达一些非常简单的内容,例如将功能设置为true,那么XML语法可能会成为负担。

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. 无法“附加”到XML属性文件以引入新属性或为现有属性提供覆盖值,以便最后一个获胜。 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 附加属性的能力可能非常强大 - 我们可以围绕此实现属性管理逻辑,以便某些属性“热”,并且当这些更改时我们不需要重新启动实例

The Java property file solves the above problems. Java属性文件解决了上述问题。 Consistent naming conventions and dot notation can help in solving the issue of hierarchy. 一致的命名约定和点符号可以帮助解决层次结构问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM