3

I have a multi-dimensional array such as:

String[][] greatCities = new String[2][2];
greatCities[0][0] = "Vancouver";
greatCities[0][1] = "Charlottetown";
greatCities[1][0] = "Zürich";
greatCities[1][1] = "Bern";

Now I'm looking for a good way to save the structure (no code) this array to a xml file. Best solution I have so far is:

<GreatCities>
  <Item index0="0" index1="0">Vancouver</Item>
  <Item index0="0" index1="1">Charlottetown</Item>
  <Item index0="1" index1="0">Zürich</Item>
  <Item index0="1" index1="1">Bern</Item>
</GreatCities>

Does anyone have a better solution?

3
  • 2
    I have a creeping suspicion that this 2D array is actually a projection of some other data structure. A simple map, maybe? A list of pairs? What does it mean if "Vancouver" is at 0,1 as opposed to 0,0? The answer would probably influence how this should be mapped. Commented Mar 15, 2011 at 15:02
  • Looks like that to me too. What information is conveyed by the arrangement of the two-dimensional table? Without knowing that, it's impossible to design an XML structure that captures the information. Commented Mar 15, 2011 at 21:37
  • I do agree with you, totally! But... (it might sound unprofessional) we do not know how the data structure behind looks like... we get a text file which is scanned line by line and must be converted into a xml structure for future use (eg. comparing and so on). It is just important that no information get lost. Commented Mar 16, 2011 at 9:39

3 Answers 3

7

As its effectively an array of arrays...

<GreatCity index =0>
   <Name index="0">Vancouver</Name>
   <Name index="1">Charlottetown</Name>
</GreatCity>
etc...
Sign up to request clarification or add additional context in comments.

1 Comment

We just had the same idea. :-) We do think it's the best way. Thank you!
2
<GreatCities>
  <Items index="0">
    <Item index="0">Vancouver</Item>
    <Item index="1">Charlottetown</Item>
  </Items>
  <Items index="1">
    <Item index="0">Zürich</Item>
    <Item index="1">Bern</Item>
  </Items>
</GreatCities>

Comments

2

Your solution may be compact enough, but I think is complicated for the deserialization (e.g. retrieve the array from the xml). In fact, you should know as soon what is the size of the array: to do that you must scan the whole xml document. I'd rather prefer a structure ordered, without attribute indexes:

<GreatCities>
  <GreatCity>
    <Name>Vancouver</Name>
    <Name>Charlottetown</Name>
  </GreatCity>
  <GreatCity />
  <GreatCity>
    <Name>Zürich</Name>
    <Name/>
    <Name>Bern</Name>
  </GreatCity>
</GreatCities>

In that sample I have inserted two empty elements to point and empty row/cell. At this point, you may scan the xml document by filling the jagged array.

1 Comment

Unfortunately we need the indexes because we are mapping a proprietary text file to an XML file scanning line by line and the array values might not be ordered in text file but we will have information about their index. However I do agree with your solution in an other situation. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.