I am currently working on a project where I get a set of data from an XML file. The XML file has multiple node that store different data types (strings, ints and bools). Java returns each of these as a string which is fine because you can convert them into the proper data types within the program. As the nodes are returned I want to put them into an array of proper type and store each of those arrays in a master array for later use in my program. In my XML file I would like to have a node where there can be multiple child nodes with the same tag which is why I am using a multi-dimensional array.
I have attempted using an Object[][]
and Variable[][]
to no success, unless I am missing something simple (which could be the case).
XML example:
<parent>
<child1>string</child1>
<child2>int</child2>
<child2>int</child2>
<child3>boolean</child3>
</parent>
Java example:
NodeList nList = doc.getElementsByTagName("parent");
NodeList nlAttributes = nList.item(cardId).getChildNodes();
List<String> lElements = new ArrayList<String>();
List<String> lRepeateElement = new ArrayList<String>();
for(int i = 0; i <nlAttributes.getLength(); i++){
if(nlAttributes.item(i).getNodeType()!=3&&nlAttributes.item(i).getNodeType()!=8){
if(nlAttributes.item(i).getNodeName().equals("effect")){
lRepeateElement.add(nlAttributes.item(i).getTextContent());
}else{
lElements.add(nlAttributes.item(i).getTextContent());
}
}
}
aRepeatElements = new String[lRepeateElement.size()];
for(int i = 0; i<lRepeateElement.size(); i++){
aRepeatElements[i]=lRepeateElement.get(i);
}
sElements = new String[lElements.size()];
oElements = new Variable[lElements.size()+1][]; //oElements = new Object[lElements.size()+1][];
for(int i = 0; i<lElements.size(); i++){
if(i==4){
oElements[i]=aRepeatElements;//incompatible types: String[] cannot be converted to Variable/Object[] -> this still exists after attempting to casts
}else{
sElements[i]=lElements.get(0);
String[] temp = new String[1];
temp[0]=sElements[i];//incompatible types: String[] cannot be converted to Variable/Object[] -> this still exists after attempting to casts
oElements[i]=temp;
lElements.remove(0);
}
}
int
toInteger
(and similarly for other primitives). – Chthonic Project Oct 1 '14 at 23:07