XML for configuration files, why? [closed]

2019-01-30 17:54发布

Why do so many projects use XML for configuration files?

12条回答
放我归山
2楼-- · 2019-01-30 18:26

The main advantage of XML and the reason why is so popular is because it's popular in java world and therefore all of the enterprise applications written in java use it, and also because web services and soap are based on xml and those are used a lot in enterprise applications.

And so far, JSON and all other formats aren't so well supported by the industry, except in ajax applications. Also, JSON does not have an schema language or an defined parsing api like XML.

Even if roughly speaking, JSON doesn't need the tons of stuff xml has, at least not in the same way, and I'm speaking in web services, when I say that...

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-30 18:26

One reason which was not specified in other answers is Unicode / text encoding / you name it. Need a chinese string in the file? No problem. This might sound trivial, but when XML was introduced it wasn't. Obviously not in INI files.

Another thing - it was the first thing that gave us possibility to have structured data with lists, dictionaries or whatever you want, which is machine-processable and human editable at the same time.

It has disadvantages, but what else could you use? Yaml looks great, but I'm afraid to introduce it in projects I work on because I just see in my imagination all those problems with people putting a white space in the wrong place, or merging tools not caring about them.

查看更多
▲ chillily
4楼-- · 2019-01-30 18:28

Thanks for your answers. This question, as naive as it may seem at first glance was not so naive :)

Personally I don't like XML for configuration files, I think it's hard for people to read and change, and it's hard for computers to parse because it's so generic and powerful.

INI files or Java propery files are fine for only the most basic applications that does require nesting. common solutions to add nesting to those formats look like:

level1.key1=value
level1.key2=value
level2.key1=value

not a pretty sight, a lot of redundancy and hard to move things between nodes.

JSON is not a bad language, but it's designed to be easy for computers to parse (it's valid JavaScript), so it's not wildly used for configuration files.

JSON looks like this:

{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}

In my opinion, it's too cluttered with commas and quotes.

YAML is good for configuration files, here is a sample:

invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars

however, I don't like its syntax too much, and I think that using the whitespace to define scopes make things a bit fragile (think pasting a block to a different nesting level).

A few days ago I started to write my own language for configuration file, I dubbed it Swush.

Here are a few sample: as a simple key-value pairs:

key:value
key:value2
key1:value3

or as a more complex and commented

server{
    connector{
         protocol : http // HTTP or BlahTP
         port : 8080     # server port
         host : localhost /* server host name*/
    }

    log{
        output{
             file : /var/log/server.log
             format : %t%s
        }
    }
}

Swush supports strings in the simple form above, or in quotes - which allows whitespaces and even newlines inside strings. I am going to add arrays soon, somethings like:

name [1 2 b c "Delta force"]

There is a Java implementation, but more implementations are welcome. :). check the site for more information (I covered most of it, but the Java API provide a few interesting features like selectors)

查看更多
一夜七次
5楼-- · 2019-01-30 18:31
  1. XML is easy to parse. There are several popular, lightweight, featureful, and/or free XML parsing libraries avaliable in most languages.
  2. XML is easy to read. It is a very human-readable markup language, so it's easy for humans to write as well as for computers to write.
  3. XML is well specified. Everyone and his dog knows how to write decent XML, so there's no confusion about the syntax.
  4. XML is popular. Somewhere along the way, some Important People™ started pushing the idea that XML was the "future", and a lot of people bought it.
  5. XML is a bidirectional format. That is whitespace, comments, and order are preserved. You can programmatically load, change and then save it while preserving the formatting. This is important for tools that users can use to configure their applications. It is one of the reasons XML originally took off (the world has become more technical so this is less of a need).
  6. XML has optional schema validation. Important for tools and complex configuration formats.
  7. XML has namespaces. This allows other configurations or annotations to be embedded with out effecting the parsing. In other configuration formats this is usually done as a with hack special comments or property name mangling.

As a side note, I'm not trying to defend XML. It has its uses, and I will be using it in a project whenever I get back to that. In many cases, though, and especially configuration files, the only advantage it has is that it's a standardized format, and I think this is far outweighed by numerous disadvantages (i.e. it's too verbose). However, my personal preferences don't matter - I was merely answering why some people might choose to use XML as a configuration file format. I personally never will.

查看更多
我命由我不由天
6楼-- · 2019-01-30 18:31

Because parsing XML is relatively easy, and if your schema is clearly specified, any utility can read and write information easily into it.

查看更多
做自己的国王
7楼-- · 2019-01-30 18:32

Here are some historical reasons:

  • The W3C moved from building tools in Perl to Java
  • The Apache foundation moved from building tools in Perl to Java
  • Java has lots of XML APIs
  • Configuration can therefore be done in Java
  • Configuration via XML and properties files is for non-Java developers

JTidy configuration vs tidy configuration is a prime example of this.

查看更多
登录 后发表回答