ramboCODER
ramboCODER

Reputation: 73

Android XML annotation

I've been using Simple to try to read in my XML file to this class. I really don't know if I've annotated the classes correctly.

I don't know if I need this part:

public Frame()
{
    super();
}

public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration )
{
    this.Num = num;
    this.X = x;
    this.Y = y;
    this.Width = width;
    this.Height = height;
    this.OffsetX = offsetx;
    this.OffsetY = offsety;
    this.Duration = duration;]

What does super() do? Do I need getters/setters? Is what I added getters or setters? Do they call themselves automatically or what?

Here's the full class:

public class SpriteAnimationManag 
{
// Animation frame class



@Element(name = "Frame")
public class Frame
{

    @Element(name = "Num")
    public int Num;


    @Element(name = "X")
    public int X;

            @Element(name = "Y")
    public int Y;


    @Element(name = "Width")
    public int Width;


    @Element(name = "Height")
    public int Height;


    @Element(name = "OffSetX")
    public int OffsetX;


    @Element(name = "OffSetY")
    public int OffsetY;


    @Element(name = "Duration")
    public float Duration;

    public Frame()
    {
        super();
    }

    public Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration )
    {
        this.Num = num;
        this.X = x;
        this.Y = y;
        this.Width = width;
        this.Height = height;
        this.OffsetX = offsetx;
        this.OffsetY = offsety;
        this.Duration = duration;
    }



}

// Animaiton class to hold the name and frames
public class Animation 
{

    @Element(name = "Name")
    public String Name;


    @Element(name = "FrameRate")
    public int FrameRate;//may need elementarray or list???

    @Element(name = "Loop")
    public boolean Loop;

    @Element(name = "Pingpong")
    public boolean Pingpong;


    @ElementArray(name = "Frames") 
    public Frame[] Frames;

public Animation()
{
    super();
}

public Animation(String name, int framerate, boolean loop, boolean pingpong, Frame[] frames)
{
this.Name = name;
this.FrameRate = framerate;
this.Loop = loop;
this.Pingpong = pingpong;
this.Frames = frames;
}


}

// The Sprite Texture stores the Sprite Sheet path.fr
public class SpriteTexture 
{
    // The Sprite Sheet texture file path

    @Element(name = "path")
    public String Path;

    public SpriteTexture()
    {
        super();
    }

    public SpriteTexture(String path)
    {
        this.Path = path;
    }

}

// Aniamtion Set contains the Sprite Texture and Animaitons.

@Root(name = "Animations")
public static class XNAAnimationSet
{
    // The sprite texture object

    @Element(name = "Texture")
    public SpriteTexture SpriteTexture;

    // The animation array in the Animation Set
        @ElementArray(name = "Animation")
    public Animation[] Animations;

    public XNAAnimationSet()
    {
        super();
    }

    public XNAAnimationSet(SpriteTexture spritetexture, Animation[] animations)
    {
        this.SpriteTexture = spritetexture;
        this.Animations = animations;
    }
}

// Sprite Animation Manager class
public final static class SpriteAnimationManager 
{
    private static final String XNAAnimationSet = null;//was static private static
    public static int AnimationCount;

    // Read the Sprite Sheet Description information from the description xml file
    public static XNAAnimationSet Read(String filename) throws Exception
    {

         XNAAnimationSet animationSet = new XNAAnimationSet();


             Serializer serializer = new Persister();

             try {
                animationSet = serializer.read(XNAAnimationSet.class, filename );

            } 
             catch (Exception e)
             {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        // Count the animations to Animation Count
        AnimationCount = animationSet.Animations.length;

        return animationSet;
    }
  }
}

I've been trying to see what's being read by trying to write the class to a file. The file is created but it's empty.

Can someone tell me if I've annotated this correctly? What am I doing wrong?

Upvotes: 0

Views: 2197

Answers (1)

Gary O' Donoghue
Gary O' Donoghue

Reputation: 372

I was using jaxb there the last day to parse my xml, I'm not sure how similiar it is to the way your doing it but ill mention a few of the things i needed:

firstly, i think i needed a no-arg constructor in my class, which for you would just be -

public Frame(){};

I believe you do need getters, what you've got there arent getters/setters, your just declaring variables, this really is fundamental java stuff so it might be worth a read up on that before you continue. When you have your getters defined properly, you then put the @XMLElement annotation above each of them, not above your variable declarators. A getter looks like:

@XMLElement    
public string getName(){ return this.Name};

Also id recommend trying to parse one class at a time, you have multiple inner classes here which i'd imagine gets messy when your trying to parse, i think you need to have @RootElement above the class name declarator, so the xml knows what type of object your creating.

Anyway, there's a few things off the top of my head, best of luck with it!

Upvotes: 1

Related Questions