我正在开发一款游戏,我想在xml文件中保存对象的状态。我没有它的架构。说我有,国家有国家对象列表,有一个城市对象列表。如何生成保存其信息的xml架构?
答案 0 :(得分:2)
见下面的例子可以帮助你,
主类 公共类CreateXML {
public static void main(String[] args) {
try {
final JAXBContext context = JAXBContext.newInstance(Country.class);
final Marshaller marshaller = context.createMarshaller();
// Create a stringWriter to hold the XML
final StringWriter stringWriter = new StringWriter();
CreateXML xml = new CreateXML();
Country country = xml.createCountry();
marshaller.marshal(country, stringWriter);
// Print out the contents of the stringWriter
System.out.println(stringWriter.toString());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private List<String> createCity(){
List<String> cities = new ArrayList<String>();
cities.add("Slough");
cities.add("Manchestor");
return cities;
}
private List<State> createState(){
List<State> states = new ArrayList<State>();
State state = new State();
state.setStatName("London");
state.setCityList(createCity());
states.add(state);
return states;
}
private Country createCountry(){
Country country = new Country("UK", createState());
return country;
}
}
2.Java Beans - 国家
@XmlRootElement
public class Country {
String countryName;
List<State> states;
public Country(String countryName, List<State> states) {
this.countryName = countryName;
this.states = states;
}
public Country() {
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public List<State> getStates() {
return states;
}
public void setStates(List<State> states) {
this.states = states;
}
}
3.Javabean State
String statName;
List<String> cityList;
public State() {
}
public String getStatName() {
return statName;
}
public void setStatName(String statName) {
this.statName = statName;
}
public List<String> getCityList() {
return cityList;
}
public void setCityList(List<String> cityList) {
this.cityList = cityList;
}
答案 1 :(得分:1)
使用XMLEncoder / Decoder我们不必创建架构,而只是以特定形式制作'beans'。例如。一个City
bean。
import java.util.ArrayList;
public class City {
public String name;
public City() {
}
public City(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
对于我们希望序列化的所有公共属性,将有一个公共'no arg的构造函数和公共getter和setter。 State
课程类似。
import java.util.ArrayList;
public class State {
public ArrayList<City> cities;
public String name;
public State() {
}
public State(String name) {
this.setName(name);
}
public ArrayList<City> getCities() {
return cities;
}
public void setCities(ArrayList<City> cities) {
this.cities = cities;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后我们只需要一个简短的代码来序列化该批次。
import java.awt.Desktop;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class SerializeObject {
public static void main(String[] args) throws Exception {
ArrayList<State> states = new ArrayList<State>();
State nsw = new State("New South Wales");
states.add(nsw);
ArrayList<City> nswCities = new ArrayList<City>();
nswCities.add(new City("Sydney"));
nswCities.add(new City("Wollongong"));
nsw.setCities(nswCities);
State qld = new State("Queensland");
states.add(qld);
ArrayList<City> qldCities = new ArrayList<City>();
qldCities.add(new City("Brisbane"));
qldCities.add(new City("Ipswich"));
qld.setCities(qldCities);
File f = new File("localities.xml");
FileOutputStream fos = new FileOutputStream(f);
XMLEncoder xmle = new XMLEncoder(fos);
xmle.writeObject(states);
xmle.close();
Desktop.getDesktop().edit(f);
System.out.println("Finished!");
}
}
生成的序列化文件。
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0" class="java.beans.XMLDecoder">
<object class="java.util.ArrayList">
<void method="add">
<object class="State">
<void property="cities">
<object class="java.util.ArrayList">
<void method="add">
<object class="City">
<void property="name">
<string>Sydney</string>
</void>
</object>
</void>
<void method="add">
<object class="City">
<void property="name">
<string>Wollongong</string>
</void>
</object>
</void>
</object>
</void>
<void property="name">
<string>New South Wales</string>
</void>
</object>
</void>
<void method="add">
<object class="State">
<void property="cities">
<object class="java.util.ArrayList">
<void method="add">
<object class="City">
<void property="name">
<string>Brisbane</string>
</void>
</object>
</void>
<void method="add">
<object class="City">
<void property="name">
<string>Ipswich</string>
</void>
</object>
</void>
</object>
</void>
<void property="name">
<string>Queensland</string>
</void>
</object>
</void>
</object>
</java>
答案 2 :(得分:0)
尝试使用xstream http://x-stream.github.io/tutorial.html
XStream是一个简单的库,用于将对象序列化为XML,然后再返回。