Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XSTest.java:
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import com.thoughtworks.xstream.XStream;
- public class XSTest {
- private static final String XML_FILE = "c:\\temp\\items.xml";
- private XStream xs;
- public XStream getXS()
- {
- if(xs == null)
- {
- xs = new XStream();
- xs.processAnnotations(Store.class);
- xs.processAnnotations(Item.class);
- xs.processAnnotations(Weapon.class);
- xs.processAnnotations(Armor.class);
- }
- return xs;
- }
- public void createModel()
- {
- Store s = new Store();
- s.items.add(new Weapon(1, "Short sword", "data/items/shortsword.png", "swords", "data/sprites/swordattack.png"));
- s.items.add(new Weapon(2, "Bastard sword", "data/items/bastardsword.png", "swords", "data/sprites/swordattack.png"));
- s.items.add(new Weapon(3, "Long sword", "data/items/longsword.png", "swords", "data/sprites/swordattack.png"));
- s.items.add(new Armor(4, "Studded leather armor", "data/items/studdedleather.png", "Chest", 20));
- save(s, XML_FILE);
- }
- public Store loadStore()
- {
- XStream xs = getXS();
- return (Store) xs.fromXML(new File(XML_FILE));
- }
- public boolean save(Object obj, String location)
- {
- try
- {
- File f = new File(location);
- File dir = f.getParentFile();
- if(!dir.exists())
- dir.mkdirs();
- XStream xs = getXS();
- String xml = xs.toXML(obj);
- FileWriter fstream = new FileWriter(f);
- BufferedWriter out = new BufferedWriter(fstream);
- out.write(xml);
- out.close();
- System.out.println(String.format("Generated xml '%s':\n%s", location, xml));
- return true;
- }
- catch(Exception ex)
- {
- return false;
- }
- }
- public static void main(String[] args)
- {
- XSTest test = new XSTest();
- test.createModel();
- Store s = test.loadStore();
- for(Item i : s.items)
- {
- System.out.println(i.toString());
- }
- }
- }
- Store.java:
- import java.util.ArrayList;
- import com.thoughtworks.xstream.annotations.XStreamAlias;
- @XStreamAlias("store")
- public class Store {
- public ArrayList<Item> items;
- public Store()
- {
- items = new ArrayList<Item>();
- }
- }
- Item.java:
- import com.thoughtworks.xstream.annotations.XStreamAlias;
- @XStreamAlias("item")
- public abstract class Item {
- public int id;
- public String name;
- public String inventorySheet;
- public Item()
- {
- }
- public Item(int id, String name, String inventorySheet)
- {
- this.id = id;
- this.name = name;
- this.inventorySheet = inventorySheet;
- }
- public abstract String getTypeName();
- @Override
- public String toString()
- {
- return String.format("%s (%d): %s", getTypeName(), id, name);
- }
- }
- Armor.java:
- import com.thoughtworks.xstream.annotations.XStreamAlias;
- @XStreamAlias("armor")
- public class Armor extends Item{
- public String armorType;
- public int armorValue;
- public Armor()
- {
- }
- public Armor(int id, String name, String sheet, String type, int value)
- {
- super(id, name, sheet);
- this.armorType = type;
- this.armorValue = value;
- }
- @Override
- public String getTypeName() {
- return "Armor";
- }
- @Override
- public String toString()
- {
- return String.format("%s, type: %s, armor value: %d", super.toString(), armorType, armorValue);
- }
- }
- Weapon.java:
- import com.thoughtworks.xstream.annotations.XStreamAlias;
- @XStreamAlias("weapon")
- public class Weapon extends Item{
- public String weaponType;
- public String attackSheet;
- public Weapon(){}
- public Weapon(int id, String name, String inventorySheet, String type, String attackSheet )
- {
- super(id, name, inventorySheet);
- this.weaponType = type;
- this.attackSheet = attackSheet;
- }
- @Override
- public String getTypeName() {
- return "Weapon";
- }
- @Override
- public String toString()
- {
- return String.format("%s, type: %s, attack sheet: %s", super.toString(), weaponType, attackSheet);
- }
- }
- The generated XML:
- <store>
- <items>
- <weapon>
- <id>1</id>
- <name>Short sword</name>
- <inventorySheet>data/items/shortsword.png</inventorySheet>
- <weaponType>swords</weaponType>
- <attackSheet>data/sprites/swordattack.png</attackSheet>
- </weapon>
- <weapon>
- <id>2</id>
- <name>Bastard sword</name>
- <inventorySheet>data/items/bastardsword.png</inventorySheet>
- <weaponType>swords</weaponType>
- <attackSheet>data/sprites/swordattack.png</attackSheet>
- </weapon>
- <weapon>
- <id>3</id>
- <name>Long sword</name>
- <inventorySheet>data/items/longsword.png</inventorySheet>
- <weaponType>swords</weaponType>
- <attackSheet>data/sprites/swordattack.png</attackSheet>
- </weapon>
- <armor>
- <id>4</id>
- <name>Studded leather armor</name>
- <inventorySheet>data/items/studdedleather.png</inventorySheet>
- <armorType>Chest</armorType>
- <armorValue>20</armorValue>
- </armor>
- </items>
- </store>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement