0

我一直在使用 Simple 尝试将我的 XML 文件读入此类。我真的不知道我是否正确地注释了这些类。

我不知道我是否需要这部分:

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

super() 是做什么的?我需要getter/setter吗?我添加的是getter还是setter?他们是自动称呼自己还是什么?

这是完整的课程:

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

我一直试图通过尝试将类写入文件来查看正在读取的内容。该文件已创建,但它是空的。

有人能告诉我我是否正确注释了吗?我究竟做错了什么?

4

1 回答 1

1

我最后一天在那里使用 jaxb 来解析我的 xml,我不确定它与您的操作方式有多相似,但我会提到一些我需要的东西:

首先,我认为我的班级中需要一个无参数构造函数,对你来说,它只是-

public Frame(){};

我相信你确实需要 getter,你所拥有的不是 getter/setter,你只是声明变量,这确实是基本的 java 东西,所以在继续之前可能值得一读。当你正确定义了你的 getter 后,你就可以把@XMLElement注解放在它们上面,而不是放在你的变量声明符上面。吸气剂看起来像:

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

我还建议尝试一次解析一个类,这里​​有多个内部类,我想当你尝试解析时会变得混乱,我认为你需要@RootElement在类名声明符之上,所以 xml 知道什么类型反对你的创作。

无论如何,我脑子里有几件事,祝你好运!

于 2012-11-17T14:03:12.010 回答