Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[JAXB API] XmlElementWrapper - No items in the Array

jaxb java xml

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 Crusher

Crusher

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 11 November 2015 - 10:41 PM

First of all: I've already ask this is another forum and basically just copied my post, so you will find this, if you search for it with google in the dreamincode forums. I've asked this a couple of weeks ago and it seems like no one could (or wanted) to help me. I've also removed all the imports, to make it the code a bit smaller here but there are no errors in my code currently.

-------------------------------

I'm currently making a text based adventure game and everything is nearly finished but somehow there seems to be a problem with a XmlElementWrapper. I have a class called Inventory, with a ArrayList, which holds the items for the rooms and for the creatures.

Here's how the XML File looks rougly. I removed a lot of sutff, because the XML File is quite huge and the other stuff most likely doesn't matter. I've also got a lot more rooms and so on, but just a example XML File:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<map>
    <rooms>
        <room id="1">
            <description>The Entrance of the map/description>
            <neighbour>
                <direction>ahead</direction>
                <ref>2</ref>
            </neighbour>
        </room>
        <room id="2">
            <description>Room 2</description>
            <neighbour>
                <direction>right</direction>
                <ref>3</ref>
            </neighbour>
            <inventory>
                <weapon>
                    <name>Sword</name>
                    <description>An old sword</description>
                    <damage>15</damage>
                </weapon>
                <consumable>
                    <name>Health Potion</name>
                    <description>Regenerated 10 health points</description>
                    <reg>10</reg>
                </consumable>
            </inventory>
        </room>
        <room id="3">
            <description>Room 3</description>
        </room>
    </rooms>
</map>

This is my Item class:

@XmlSeeAlso({Consumable.class, Weapon.class})
public abstract class Item
{
    private String name;
    private String description;

    public Item()
    {
 
    }

    public Item(String name, String description)
    {
        this.name = name;
        this.description = description;
    }

    public String getName()
    {
        return name;
    }

    public String getDescription()
    {
        return description;
    }

    @XmlElement(name = "name")
    public void setName(String name)
    {
        this.name = name;
    }

    @XmlElement(name = "description")
    public void setDescription(String description)
    {
        this.description = description;
    }

    public boolean isWeapon()
    {
        return this.getClass() == Weapon.class;
    }

    public boolean isConsumable()
    {
        return this.getClass() == Consumable.class;
    }
}

and this one of the subclasses, just here one for example: (The Weapon class)

@XmlRootElement(name = "weapon")@XmlAccessorType(XmlAccessType.NONE)
public class Weapon extends Item
{
    private int damage;

    public Weapon()
    {

    }

    public Weapon(String name, String description, int damage)
    {
        super(name, description);
        this.damage = damage;
    }

    public int getDamage()
    {
        return damage;
    }

    @XmlElement(name = "damage")
    public void setDamage(int damage)
    {
        this.damage = damage;
    }
}

and this is the inventory class:

@XmlRootElement(name = "inventory")@XmlAccessorType(XmlAccessType.NONE)
public class Inventory
{    
    @XmlElementWrapper(name = "inventory")@XmlElement(name = "item")
    private ArrayList<Item> inventory;
    
    public Inventory()
    {   
        System.out.println("Inventory Constructor has been used!");
        inventory = new ArrayList<Item>();
    }
    
    public String getAll()
    {
        String returnString = "";
        
        for(Item item : inventory)
        {
            returnString += " " + item.getName();
        }
        
        return returnString;     
    }
   
    public Item get(String name)
    {
        return get(name);
    }
   
    public void add(Item item)
    {
        inventory.add(item);
    }

    public Item remove(String name)
    {
        Item item = this.get(name);
        inventory.remove(this.get(item));
        return item;
    }
    
    public void removeAll()
    {
        if(inventory.size() > 0)
        {
            inventory.clear();
            System.out.println("All items of have been removed of your inventory!");
        }
        else
        {
            System.out.println("There are no items in your inventory!");
        }
    }      
}

So, i've also added the System.out.println to the constructor, so that i can see if the constructor has been used and i get as many printlines as i've got rooms, so the constructor is used for each room. But there are always no items in the inventory, like it seems.

Here's the room class:

@XmlRootElement(name = "room")@XmlAccessorType(XmlAccessType.NONE)
public class Room
{
    @XmlID
    @XmlAttribute
    private String id;

    private String description;
    private Inventory inventory;

    @XmlElementWrapper(name = "neighbours")@XmlElement(name = "neighbour")
    private Neighbour[] neighbours;

    private static final int directionNum = Direction.values().length;

    public Room()
    {
        neighbours = new Neighbour[directionNum];
    }

    public String geDescription()
    {
        return "Description of the Room: " + description + ".\n" + getNeighbours() + "\nItems in this room: " + inventory.getAll();
    }

    @XmlElement(name = "description")
    public void setDescription(String description)
    {
        this.description = description;
    }
       
    public void addItem(Item item)
    {
        inventory.add(item);
    }

    public Item getItem(String name)
    {
        return (Item) inventory.get(name);
    }

    public Item removeItem(String name)
    {
        return (Item) inventory.remove(name);
    }
}[

So, what am i doing wrong? I don't get it. I always use the getAll() method of the room class to get every item in that room, if i change the room but there are always no items in the ArrayList, like it seems.

Another question btw:
I also would like to have two items of the same type in the ArrayList, like two health potions, one that gives back 10 health and one that gives 20 health back. How can i ensure then, if i use the get method, that i get the right item, or if use the remove method, that i remove the right item and not the false one?


Edited by Crusher, 11 November 2015 - 10:46 PM.


#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts

Posted 11 November 2015 - 11:12 PM

Short one cause im on phone,

I think it's wrong because you have both xmlrootelement 'inventory' a s well as axML element wrapper 'inventory'.

Try removing the xmlelementwrapper

#3 Crusher

Crusher

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 12 November 2015 - 03:48 AM

That's what i also fhought first after i checked a lot of other stuff but i still doesn't work.



#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts

Posted 12 November 2015 - 04:35 AM

Did you also try List instead of ArrayList and otherwise just write out xml with your current classes and compare it to the xml you try to read.



#5 Crusher

Crusher

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 13 November 2015 - 02:26 PM

Yes, i've also tried to use a List instead of an ArrayList, but that shouldn't make a difference? If it would now write out the XML, i would just get the actual stuff, that i've created with the XML File, because i don't create any game objects within the game class. I don't see any sense in that.



#6 Crusher

Crusher

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 19 November 2015 - 05:40 PM

Seems like again no one could help me, to solve the problem? ;(