从XML中读取二维

时间:2013-08-22 22:07:30

标签: java xml arrays

我试图将以下XML文件读入二维数组,其中x是第一个Arraylist的索引,Y是第二个Arraylist的索引。 (带坐标的地图)

<?xml version="1.0" encoding="UTF-8"?>

<root>
    <Tile w="0" x="0" y="0">
        <mesh>grass.png</mesh>
    </Tile>
    <Tile w="0" x="0" y="1">
        <mesh>grass.png</mesh>
    </Tile>
    <Tile w="0" x="0" y="2">
        <mesh>grass.png</mesh>
    </Tile>
    <Tile w="0" x="0" y="3">
        <mesh>grass.png</mesh>
    </Tile>
</root>

代码

    private static ArrayList<ArrayList<Tile>> loadTiles( Element root ) throws Exception {
    ArrayList<ArrayList<Tile>> result = new ArrayList<>();

    List channels = root.getChildren();       
    ListIterator it2 = channels.listIterator();
    while (it2.hasNext()){
        //- Tile
        Element el = (Element)it2.next();

        int x = el.getAttribute("x").getIntValue();
        int y = el.getAttribute("y").getIntValue();
        boolean w = el.getAttribute("w").getBooleanValue();
        Tile t = new Tile(new Point(x,y), w);
        //- Children
        t.setMesh(el.getValue().trim()); //hardcoded
        if (result.get(x) == null) result.add(new ArrayList<Tile>());
        result.get(x).set(y, t);

     }

    return result;
}

任何想法我应该如何将它加载到数组中,而不是像我得到atm那样的任何无效点?

0 个答案:

没有答案