JAXB 怎么样?看看编写配置和读回它是多么容易:
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
class Config {
@XmlElementWrapper(name="monsters")
@XmlElement(name="monster")
public List<Monster> monsters = new ArrayList<Monster>();
}
class Monster {
public String name = "Test";
}
public class Test1 {
public static void main(String[] args) throws Exception {
Config cfg = new Config();
cfg.monsters.add(new Monster());
//save
OutputStream os = new FileOutputStream("conf.xml");
JAXB.marshal(cfg, os);
// read
cfg = JAXB.unmarshal(new FileInputStream("conf.xml"), Config.class);
}
}
配置文件
<config>
<monsters>
<monster>
<name>Test</name>
</monster>
</monsters>
</config>