开发者

How to structure a multi-dimensional array in a XML file?

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 开发者_StackOverflow社区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?


As its effectively an array of arrays...

<GreatCity index =0>
   <Name index="0">Vancouver</Name>
   <Name index="1">Charlottetown</Name>
</GreatCity>
etc...


<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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜