是否有一个XML构建器/解析器将用数据填充java模型类?

时间:2015-03-18 21:04:59

标签: java xml parsing builder

我一直在stackoverflow和google上搜索大约20分钟,并且可以找到解决方案。我可以编写自己的XSD生成器,然后编写XML构建器,但我不能相信没有一些框架/库可以做到这一点。

也许你知道一些......

为了更好地理解,我已经编写了这些文件:

XML:

<root>
    <spritesheets>
        <spritesheet id="1" spritecount="16" spriteWidth="32" spriteHeight="32" source="spritesheets/sprites_grass.png" />
        <spritesheet id="2" spritecount="24" spriteWidth="64" spriteHeight="64" source="spritesheets/sprites_trees.png" />
        <spritesheet id="3" spritecount="24" spriteWidth="64" spriteHeight="64" source="spritesheets/sprites_bigtrees.png" />
        <spritesheet id="4" spritecount="12" spriteWidth="128" spriteHeight="128" source="spritesheets/sprites_mothertree.png" />
    </spritesheets>

    <animations>
        <animation id="1" name="Grass Moving 1" spritesheet_id="1" from="1" to="8" period="200" />
        <animation id="2" name="Grass Moving 2" spritesheet_id="1" from="9" to="16" period="200" />
        <animation id="3" name="Grass Moving Slowly 1" spritesheet_id="1" from="1" to="8" period="350" />
        <animation id="4" name="Grass Moving Slowly 2" spritesheet_id="1" from="9" to="16" period="350" />
        <animation id="5" name="Tree 1" spritesheet_id="2" from="1" to="12" period="200" />
        <animation id="6" name="Tree 2" spritesheet_id="2" from="13" to="24" period="200" />
        <animation id="7" name="Big Tree 1" spritesheet_id="3" from="1" to="12" period="200" />
        <animation id="8" name="Big Tree 2" spritesheet_id="3" from="13" to="24" period="200" />
        <animation id="9" name="Mother Tree" spritesheet_id="4" from="1" to="24" period="250" />
    </animations>

    <objects>
        <walkable id="1" name="Jungle Grass 1" width="1" height="1" animation_id="1" />
        <walkable id="2" name="Jungle Grass 2" width="1" height="1" animation_id="2" />
        <walkable id="3" name="Jungle Grass 3" width="1" height="1" animation_id="3" />
        <walkable id="4" name="Jungle Grass 4" width="1" height="1" animation_id="4" />
        <placeable id="5" name="Tree 1" width="1" height="1" animation_id="5" />
        <placeable id="6" name="Tree 2" width="1" height="1" animation_id="6" />
        <placeable id="7" name="Big Tree 1" width="2" height="2" animation_id="7" />
        <placeable id="8" name="Big Tree 2" width="2" height="2" animation_id="8" />
        <placeable id="9" name="Mother Tree" width="3" height="3" animation_id="9" />
        <monster... />
    </objects>

    <locations>
        <location id="1" name="Jungle" width="500" height="500">
            <object object_ref_id="1" posX="0" posY="0" />
            <object object_ref_id="1" posX="0" posY="1" />
            <object object_ref_id="2" posX="0" posY="2" />
            <object object_ref_id="4" posX="0" posY="3" />
            ...
            <object object_ref_id="9" posX="250" posY="250" />
            ...
            <object object_ref_id="3" posX="500" posY="499" />
            <object object_ref_id="3" posX="500" posY="500" />
        </location>
    </locations>
</root>

Java类:

根:

public class Root {
    List<Spritesheet> spritesheets;
    List<Animation> animations;
    List<Object> objects;
    List<Location> locations;
}

Spritesheet:

public class Spritesheet {
    int id;
    int spritecount;
    int spriteWidth;
    int spriteHeight;
    String source;
}

动画:

public class Animation {
    int id;
    String name;
    int spritesheet_id;
    int from;
    int to;
    int period;
}

对象

public abstract class Object {
    int id;
    String name;
    int width;
    int height;
    int animation_id;
}

可行走:

public class Walkable extends Object {

}

可放置:

public class Placeable extends Object {

}

位置:

public class Location {
    List<Object> objects;
    int id;
    String name;
    int width;
    int height;
}

如何简单地将XML转换为java类?不需要XSD验证,但将来会很好。

1 个答案:

答案 0 :(得分:0)

有些商业工具允许在他们的工具中执行此操作。例如,我使用了Tibco ActiveMatrix Designer,它允许您将xml文件(基于xsd文件)映射到Java实例对象中。实际上,在设计时,您可以将字段从xml拖放到java对象。然后,您可以在执行Java代码的活动中使用此对象。 也许它不会为你工作,因为你需要将生成的耳朵部署到他自己的管理员服务器中。 我想还有其他类似的解决方案。