Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为我的java对象创建xml文件_Java_Xml_Xml Parsing - Fatal编程技术网

为我的java对象创建xml文件

为我的java对象创建xml文件,java,xml,xml-parsing,Java,Xml,Xml Parsing,我正在开发一个游戏,我想把对象的状态保存在xml文件中。我没有它的模式。假设我有,Country有一个States对象列表,而Country有一个cities对象列表。如何生成保存其信息的xml模式?尝试使用xstream XStream是一个简单的库,用于将对象序列化为XML并再次序列化。尝试使用XStream XStream是一个简单的库,可以将对象序列化为XML,然后再序列化回来。请参阅下面的示例 主类 公共类CreateXML{ public static void main(Strin

我正在开发一个游戏,我想把对象的状态保存在xml文件中。我没有它的模式。假设我有,Country有一个States对象列表,而Country有一个cities对象列表。如何生成保存其信息的xml模式?

尝试使用xstream

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

尝试使用XStream


XStream是一个简单的库,可以将对象序列化为XML,然后再序列化回来。

请参阅下面的示例

  • 主类 公共类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;
    }
    
    publicstaticvoidmain(字符串[]args){
    试一试{
    final JAXBContext context=JAXBContext.newInstance(Country.class);
    final Marshaller=context.createMarshaller();
    //创建一个stringWriter来保存XML
    最终StringWriter StringWriter=新StringWriter();
    CreateXML=新建CreateXML();
    Country-Country=xml.createCountry();
    马歇尔(国家,作家);
    //打印出stringWriter的内容
    System.out.println(stringWriter.toString());
    }捕获(JAXBEException e){
    //TODO自动生成的捕捉块
    e、 printStackTrace();
    }
    }
    私有列表createCity(){
    List cities=new ArrayList();
    城市。添加(“泥沼”);
    城市。添加(“曼彻斯特”);
    回归城市;
    }
    私有列表createState(){
    列表状态=新的ArrayList();
    状态=新状态();
    州名称(“伦敦”);
    state.setCityList(createCity());
    添加(州);
    返回状态;
    }
    私人国家{
    国家=新国家(“英国”,createState());
    返回国;
    }
    
    }

  • 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;
        }
    
    }
    
    @XmlRootElement
    公营国家{
    字符串countryName;
    列出国家名单;
    公共国家(字符串countryName,列表状态){
    this.countryName=countryName;
    this.states=状态;
    }
    公共国家(){
    }
    公共字符串getCountryName(){
    返回国家名称;
    }
    public void setCountryName(字符串countryName){
    this.countryName=countryName;
    }
    公共列表getStates(){
    返回状态;
    }
    公共无效集合状态(列表状态){
    this.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;
    }
    
    字符串statName;
    列出城市名单;
    公共国家(){
    }
    公共字符串getStatName(){
    返回statName;
    }
    public void setStatName(字符串statName){
    this.statName=statName;
    }
    公共列表getCityList(){
    返回城市列表;
    }
    public void setCityList(列表cityList){
    this.cityList=城市列表;
    }
    
    请参见下面的示例帮助您

  • 主类 公共类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;
    }
    
    publicstaticvoidmain(字符串[]args){
    试一试{
    final JAXBContext context=JAXBContext.newInstance(Country.class);
    final Marshaller=context.createMarshaller();
    //创建一个stringWriter来保存XML
    最终StringWriter StringWriter=新StringWriter();
    CreateXML=新建CreateXML();
    Country-Country=xml.createCountry();
    马歇尔(国家,作家);
    //打印出stringWriter的内容
    System.out.println(stringWriter.toString());
    }捕获(JAXBEException e){
    //TODO自动生成的捕捉块
    e、 printStackTrace();
    }
    }
    私有列表createCity(){
    List cities=new ArrayList();
    城市。添加(“泥沼”);
    城市。添加(“曼彻斯特”);
    回归城市;
    }
    私有列表createState(){
    列表状态=新的ArrayList();
    状态=新状态();
    州名称(“伦敦”);
    state.setCityList(createCity());
    添加(州);
    返回状态;
    }
    私人国家{
    国家=新国家(“英国”,createState());
    返回国;
    }
    
    }

  • 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;
        }
    
    }
    
    @XmlRootElement
    公营国家{
    字符串countryName;
    列出国家名单;
    公共国家(字符串countryName,列表状态){
    this.countryName=countryName;
    this.states=状态;
    }
    公共国家(){
    }
    公共字符串getCountryName(){
    返回国家名称;
    }
    public void setCountryName(字符串countryName){
    this.countryName=countryName;
    }
    公共列表getStates(){
    返回状态;
    }
    公共无效集合状态(列表状态){
    this.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;
    }
    
    字符串statName;
    列出城市名单;
    公共国家(){
    }
    公共字符串getStatName(){
    返回statName;
    }
    public void setStatName(字符串statName){
    this.statName=statName;
    }
    公共列表getCityList(){
    返回城市列表;
    }
    public void setCityList(列表cityList){
    this.cityList=城市列表;
    }
    
    使用XMLEncoder/Decoder,我们不必创建模式,只需以特定形式生成“bean”。例如,一个
    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。
    状态
    类将类似

    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.util.ArrayList;
    公共阶级国家{
    公共阵列城市;
    公共字符串名称;
    公共国家(){
    }
    公共状态(字符串名称){
    这个.setName(name);
    }
    公共阵列列表getCities(){
    回归城市;
    }
    公共城市(ArrayList城市){
    这个。城市=城市;
    }
    公共字符串getName(){
    返回名称;
    }
    公共void集合名(字符串名){
    this.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!");
        }
    }
    
    导入java.awt.Desktop;
    导入java.beans.xmlcoder;
    导入java.io.File;
    导入java.io.FileOutputStream;
    导入java.util.ArrayList;
    公共类序列化对象{
    公共静态void main(字符串[]args)引发异常{
    ArrayList states=新的ArrayList();
    陈述