0

我正在开发一个游戏,我想将对象的状态保存在 xml 文件中。我没有它的架构。假设我有,Country 有States 对象列表,并且有Cities 对象列表。如何生成保存其信息的 xml 架构?

4

3 回答 3

2

看下面的例子可以帮助你,

  1. 主类公共类 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状态

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;
}
于 2012-10-07T06:28:07.130 回答
1

使用 XMLEncoder/Decoder,我们不必创建模式,而只需以特定形式制作“bean”。EG 一个City豆子。

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

对于我们希望序列化的所有公共属性,将有一个 public '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> 
于 2012-10-07T07:07:18.700 回答
0

尝试使用 xstream http://x-stream.github.io/tutorial.html

XStream 是一个简单的库,用于将对象序列化为 XML 并再次返回。

于 2012-10-07T05:17:13.937 回答