Read 2-dimensional from XML

I am trying to read following XML file into a 2-dimensional array where the x is the index of the first Arraylist, and the Y the index of the second. (A map with coordinates)

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

Any idea how I should load it into the array without any nullpointers like I get atm?

Answers


Need Your Help

Querying Intersystem Caché through ODBC

c# sql .net odbc intersystems-cache

I'm querying Caché for a list of tables in two schemas and looping through those tables to obtain a count on the tables. However, this is incredibly slow. For instance, 13 million records took 8 ho...

CryptographicException: Error occurred while decoding OAEP padding

c# c#-4.0 cryptography rsa

In continuing of Error occurred while decoding OAEP padding

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.