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

15,680

Solution 1

As its effectively an array of arrays...

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

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

Solution 3

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.

Share:
15,680
Thomas
Author by

Thomas

Thomas Zuberbühler | Swiss Expat in Canada | www.geomo.ch Python, Java, JavaScript, TypeScript, C# and more. Passion for maps and spatial applications. Coffee is always appreciated... ;) https://www.buymeacoffee.com/moosetraveller I am mostly just here to answer questions. I think the Stackoverflow system is flawed and questioners are sometimes treated unfriendly, arrogant and unpleasant by some users especially towards non-English speaker. Just because someone earned 10,000 reputation from a beginner's question they asked 10 years ago, it's not ok in my opinion to treat new beginners with disrespect by downvoting and close their question within seconds. I've seen this over and over again. Give beginners a chance!! And accept that not everyone's first language is English and is as good as you to express themselves in a foreign language. Ask for clarification before you downvote or close a question! Also, closing questions because they are supposed to be duplicated (even if they are slightly different) should not be allowed in my opinion. Stackoverflow should rather provide a list of possible duplicated questions. In the FAQ it is even mentioned that a duplicate question can be a signpost for other questions. Maybe this even triggers someone to give an even better or newer answer who would otherwise never came across with this or the duplicated question! Finally, why are outdated answers (old but with a lot of upvotes) favoured over the new ones? Anyways, I am here to help people and when I answer a question, I mostly learn something as well. That said, thank you for asking and helping me to improve my skills. :)

Updated on June 04, 2022

Comments

  • Thomas
    Thomas almost 2 years

    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?