Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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);
    }
}
share|improve this question
    
Object[][] should work after converting int to Integer (and similarly for other primitives). – Chthonic Project Oct 1 '14 at 23:07
    
Yup that worked... like I said above, something simple that I was missing. Thanks a lot! – TehNoiseBomb Oct 1 '14 at 23:12
    
Good to know :-) ... converted my comment to an answer. – Chthonic Project Oct 1 '14 at 23:25
    
Thanks, was looking at your profile page trying to find out how to give you credit for answering this :D – TehNoiseBomb Oct 1 '14 at 23:34

1 Answer 1

up vote 0 down vote accepted

Object[][] will solve the problem because everything in Java is an Object. Everything except the primitives, that is!

To overcome that issue, all you need to do is wrap the primitives:

  • int --> Integer
  • boolean --> Boolean
  • ...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.