title: Is it possible to serialize multiple Java class objects into a single XML file using multiple "root" elements?
body: <p>I'm working with a fairly complex and extensive number of user settings for an application. Rather than have one single megalithic class responsible for every single setting, I've judiciously decided to break it down into several smaller, simpler classes that will each be responsible for a certain set of user settings.</p>
  
  <p>My desire is to store each of these settings instances inside a single XML file for the sake of simplicity (i hope) and to keep the number of files from getting stupidly huge.</p>
  
  <p>So we come to my question:
  If I have an XML file with a single root element, is it possible to store multiple objects inside that same root element, and extract them later, OR even more ideally (for the sake of making it more readable), is it possible for me to store multiple objects in a single XML file under different root element names? For example: could the following</p>
  
  <pre><code>@XMLRootElement(name = "Settings")
  public class EQLGameSettings{
      @XmlElement(name="PlayerCount")
      final private Integer PlayerCount;
      @XmlElement(name="GameMode")
      final private EQLGameMode Mode;
      @XmlElement(name="WinnerOnNewGame")
      final private Boolean WinnerOnNewGame;
      .
      .
      .
  }
  </code></pre>
  
  <p>be stored in the same XML file alongside this under the same root element, and both be deserialized later?</p>
  
  <pre><code>@XMLRootElement(name = "Settings")
  public class EQLScoreSettings{
      @XmlElement(name="ScoreValue")
      final private Integer ScoreValue;
      @XmlElement(name="PenaltyImposed")
      final private Integer PenaltyImposed;
      @XmlElement(name="VictoryScore")
      final private Integer VictoryScore;
      .
      .
      .
  }
  </code></pre>
  
  <p>And then be able to deserialize them both into new instances of their respective classes:</p>
  
  <pre><code>JAXBContext EQLGameSettingsContext = JAXBContext.newInstance("EQLGameSettings"),
              EQLScoreSettingsContext = JAXBContext.newInstance("EQLScoreSettings");
  
  Unmarshaller UM = EQLGameSettingsContext.createUnmarshaller();
  
  File settingsXML = new File("path/to/Settings.xml");
  EQLGameSettings = (EQLGameSettings) UM.unmarshal(settingsXML);
  
  //And then be able to use the same file with and get a Score Settings instance like so:
  UM = EQLScoreSettingsContext.createUnmarshaller();
  EQLScoreSettings = (EQLScoreSettings) UM.unmarshal(settingsXML);
  </code></pre>
  
  <p>Would that work? Or would even trying to store multiple objects in the same XML file rewrite it every single time? Or would the following work?</p>
  
  <pre><code>@XMLRootElement(name = "GameSettings")
  public class EQLGameSettings{
      .
      .
      .
  }
  
  @XMLRootElement(name = "ScoreSettings")
  public class EQLScoreSettings{
      .
      .
      .
  }
  
  JAXBContext EQLGameSettingsContext = JAXBContext.newInstance("EQLGameSettings"),
              EQLScoreSettingsContext = JAXBContext.newInstance("EQLScoreSettings");
  
  Unmarshaller UM = EQLGameSettingsContext.createUnmarshaller();
  
  File settingsXML = new File("path/to/Settings.xml");
  EQLGameSettings = (EQLGameSettings) UM.unmarshal(settingsXML);
  
  //And then be able to use the same file with and get a Score Settings instance like so:
  UM = EQLScoreSettingsContext.createUnmarshaller();
  EQLScoreSettings = (EQLScoreSettings) UM.unmarshal(settingsXML);
  </code></pre>
  
  <p>Would any or all of the preceding work at all, or am I just going to be stuck with having to generate a single XML file for each settings instance?</p>
  
  <p><strong>EDIT:</strong> Looks like that's not an option. I still really do not want to go through the pain of maintaining an XML file for each settings class. My next thought is to take each setting class and instead make it serializable. So instead of trying to fit each setting into an XML file I could instead do something like so:</p>
  
  <pre><code>@XmlRootElement("Settings")
  public class MasterSettings{
      @XmlElement("GameSettings")
      EQLGameSettings GameSettings;
      @XmlElement("ScoreSettings")
      EQLScoreSettings ScoreSettings;
  }
  
  JAXBContext SettingsContext = JAXBContext.newInstance("MasterSettings");
  Unmarshaller UM = SettingsContext.createUnmarshaller();
  File SettingsFile = new File("Path/To/Settings.xml");
  
  MasterSettings Settings = (MasterSettings)UM.unmarshal(SettingsFile);
  </code></pre>
  
  <p>I'm pretty confident that will work but then the next question I need to ask is how do I set up a class so that it can be serialized in this fashion?</p>