如何轻松地将基于 XML 的配置文件加载到 Java 类中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/501325/
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-10-29 12:40:08  来源:igfitidea点击:

How to easily load a XML-based Config File into a Java Class?

javaxmlcode-injection

提问by JoshuaD

I've got a simple java class that looks something like this:

我有一个简单的 java 类,看起来像这样:

public class Skin implements Serializable {

    public String scoreFontName = "TahomaBold";
    ...
    public int scoreFontHeight = 20;
    ...
    public int blockSize = 16;
            ...

    public int[] nextBlockX = {205, 205, 205, 205};
            ...
    public String backgroundFile = "back.bmp";
            ... 
}



I'd like to read this information from a simple XML file that looks something like this:



我想从一个简单的 XML 文件中读取此信息,该文件如下所示:

<xml>
    <skin>
        <scoreFontName>"Tahoma Bold"</scoreFontName>
        ...
        <scoreFontHeight>20</scoreFontHeight>
        ...
        <blockSize>16</blockSize>
        ...
        <nextBlockX>
             <0>205</0>
             <1>205</1>
             <2>205</2>
             <3>205</3>
        <nextBlockX>
        ....
        <backgroundFile>"back.bmp"</backgroundFile>
        ...
     <skin>
 </xml>

Is there an easy way to inject the information from the xml file directly into the variable names rather than having to parse it manually? I don't mind using an external library.

有没有一种简单的方法可以将 xml 文件中的信息直接注入变量名称中,而不必手动解析它?我不介意使用外部库。

Any help is appreciated.

任何帮助表示赞赏。

回答by

XStream is really great library for just this.

XStream 是一个非常棒的库。

http://x-stream.github.io/

http://x-stream.github.io/

You can set up aliases for your class and even custom data formats to make the XML file more readable.

您可以为您的类甚至自定义数据格式设置别名,以使 XML 文件更具可读性。

回答by Fabian Steeg

Alternatives to the already mentioned solutions (XStreamand Apache Commons Digester) would be Java's own JAXBfor a comparable general approach, or solutions more tailored towards configuration like Apache Commons Configurationor Java's Preferences API.

已经提到的解决方案(XStream和 Apache Commons Digester)的替代方案是 Java 自己的JAXB,用于类似的通用方法,或者更适合配置的解决方案,如 Apache Commons Configuration或 Java 的Preferences API

回答by Uri

I would actually recommend Apache Digester, since if your classes are beans, it will just handle reading the XML into them.

我实际上会推荐 Apache Digester,因为如果您的类是 bean,它只会处理将 XML 读入其中。

回答by Fortyrunner

You can also use Spring - although it may be a bit of overkill for one class if its for a mobile game!

您也可以使用 Spring - 尽管对于一个类来说,如果它用于手机游戏可能有点矫枉过正!

回答by Paul P M

I've recently started using Simple http://simple.sourceforge.net/for XML to object mapping. It uses annotations within the class similarly to method I use in C# and is nice and clean. You can also break up the file into multiple classes and loading saving is a simple one liner.

我最近开始使用 Simple http://simple.sourceforge.net/进行 XML 到对象的映射。它在类中使用注释与我在 C# 中使用的方法类似,并且非常干净。您还可以将文件分解为多个类,加载保存是一个简单的单行。

In your case the Class structure would look like.

在您的情况下,类结构看起来像。

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root (Name="skin")
public class Skin {

    @Element(name="scoreFontName")  // name param not needed if class field is the same as the xml (element) value
    private String scoreFontName 

    @Element
    private int scoreFontHeight 

    @Element
    private int blockSize 

    @ElementArray
    private int[] nextBlockX 

    @Element    
    private String backgroundFile 

    // getters
} 

Note one difference is that your array will be saved like this

请注意,一个区别是您的数组将像这样保存

<nextBlockX>
     <int>205</int>
     <int>205</int>
     <int>205</int>
     <int>205</int>
<nextBlockX>

But you you have the option of adding (entry="myName") to the annotation in which came the XML will be saved down like this

但是您可以选择将 (entry="myName") 添加到注释中,XML 将像这样保存下来

<nextBlockX>
     <myName>205</myName>
     <myName>205</myName>
     <myName>205</myName>
     <myName>205</myName>
<nextBlockX>

But the end result is the same once it's loaded into the array.

但是一旦加载到数组中,最终结果是相同的。

回答by user3130667

Use JAXB. It is included in the JDK starting with Java 1.6.

使用 JAXB。从 Java 1.6 开始,它包含在 JDK 中。