Java: Converting Variable Multidimensional ArrayList to Array

Started by
0 comments, last by larsbutler 12 years, 3 months ago
Hello,

I have created a xml parser that parses a xml file containing grid with x/y/z information. Since the size of the 3D array is variable I chose to use ArrayList instead of Array. What I would like to do is convert the ArrayList once it has finished parsing the XML file into a 3D array. The 3D array can be jagged. Ex: in array [x][y][z], the max length of y can be 3 for some but 1 for others. What is the best way to go about this? What I ultimately want to do is just store the xyz info from the XML file into a 3D array instead of an arraylist.



public class XmlMapParser {
public XmlMapParser() {}

public static void parse(String xmlFile) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xmlFile);
doc.getDocumentElement().normalize();
DebugConsole.print("Root element " + doc.getDocumentElement().getNodeName());
DebugConsole.print("-------------------------------------------------------\n");
NodeList mapList = doc.getElementsByTagName("map");
DebugConsole.print("Information of all maps");

ArrayList grid = new ArrayList();

for (int i = 0; i < mapList.getLength(); i++) {
Node mapNode = mapList.item(i);
DebugConsole.print(mapNode.getNodeName() + " " + i);

NodeList gridList = ((Element) mapNode).getElementsByTagName("grid");
Node gridNode = gridList.item(0);
if(gridNode.getNodeType() == Node.ELEMENT_NODE) {
DebugConsole.print(gridNode.getNodeName());

NodeList xList = gridNode.getChildNodes();
int x = 0;
for(int j = 0; j < xList.getLength(); j++) {
Node xNode = xList.item(j);
if(xNode.getNodeType() == Node.ELEMENT_NODE) {
DebugConsole.print(xNode.getNodeName() + " " + j);
grid.add(new ArrayList());

NodeList yList = xNode.getChildNodes();
int y = 0;
for(int k = 0; k < yList.getLength(); k++) {
Node yNode = yList.item(k);
if(yNode.getNodeType() == Node.ELEMENT_NODE) {
DebugConsole.print(yNode.getNodeName() + " " + k);
((ArrayList)grid.get(x)).add(new ArrayList());


NodeList zList = yNode.getChildNodes();
int z = 0;
for(int l = 0; l < zList.getLength(); l++) {
Node zNode = zList.item(l);
if(zNode.getNodeType() == Node.ELEMENT_NODE) {
DebugConsole.print(zNode.getNodeName() + " " + l);
DebugConsole.print(zNode.getTextContent());
((ArrayList)((ArrayList)grid.get(x)).get(y)).add(zNode.getTextContent());
z++;
}
}
y++;
}
}
x++;

}
}
}
}

} catch (Exception e) {
DebugConsole.print("Exception: XML Parsing Error: "+e.getMessage());
}
}


This is the XML file:


<data>
<map id="0">
<grid>
<x>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
</x>
<x>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>02</z>
<z>02</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
</x>
<x>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
</x>
<x>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>02</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
</x>
<x>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
</x>
<x>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
<y>
<z>01</z>
</y>
</x>
</grid>
</map>
</data>
Advertisement
Okay, I don't know if this forum frowns upon spoon-fed answers, but...

These should do the trick, if I understand your problem correctly:
http://pastie.org/3087409 -> Bar.java
http://pastie.org/3087418 -> BarTest.java

As you can see, parameterizing the list type helps a bit (List<List<List<Double>>> grid), even though it's a bit ugly. If you don't do that, you'll drive yourself nuts with type checking and casting.

This topic is closed to new replies.

Advertisement