如何轻松地将基于XML的配置文件加载到Java类? [英] How to easily load a XML-based Config File into a Java Class?

查看:214
本文介绍了如何轻松地将基于XML的配置文件加载到Java类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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";
            ... 
}




我想从一个看起来像这样的简单XML文件中读取这些信息:



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

<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>

是否有一种简单的方法可以将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.

感谢任何帮助。

推荐答案

XStream真的很棒。

XStream is really great library for just this.

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

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

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

这篇关于如何轻松地将基于XML的配置文件加载到Java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆