Question: I've read all of the reasons why Singletons cause problems in code, but I can't find an alternative in the following scenario.

I have a Java Swing Application. The user can set settings through the GUI that effect both the display and functionality of the application, and these settings are stored and retrieved from an XML config file. When the application is loaded, a SettingsManager object is constructed. In the constructor, the settings manager parses the XML config file and stores all of the settings locally for quick access (I'll call this a cache). When a setting is changed in the application, the setting is immediately written to file but the cache is updated at the same time.

The Problem
===========

If multiple instance of the settings manager are created, when one setting is changed in one instance, the cache of the others becomes stale. Without using a singleton, one possible way to fix this would be to not use a cache and simply always retrieve settings from the file. This isn't a horrible idea, but it's not preferable. If I did this I think I'd have to put in some extra work to make it thread safe as well.

Why a Singleton helps
=====================

If SettingsManager is Singleton, there is only one cache, so it can never be stale. However, I can see already that this isn't a good idea as it's now essentially a global variable, and classes that don't need to access settings can now access them. And from what I've been reading there's plenty of other problems.

So is there some other way to design this that solves the problem without using a Singleton?

Answer: 
My favorite method for parsing xml is to use JAXB. You need an XML Schema Definition file that lays out how the XML will be structured. Then you use JAXB to generate Java classes that represent the elements. Then you can Marshal and Unmarshal XML/Data into and out of those objects.

```
//Read an XML file (or string, stream, etc...) into Java classes
JAXBContext context = JAXBContext.newInstance("org.my.jaxb.classes");
Unmarshaller unmarshaller = context.createUnmarshaller();

JAXBElement<ArrayOfSeriesRecord> rootJaxb = (JAXBElement<ArrayOfSeriesRecord>)unmarshaller.unmarshal(new File("file.xml"));
ArrayOfSeriesRecord root = rootJaxb.getValue();
List<SeriesRecord> seriesRecords = root.getSeriesRecord();

```

Example of the XSD

```
//Example xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="ArrayOfSeriesRecord" type="ArrayOfSeriesRecordType"></xsd:element>

    <xsd:complexType name="ArrayOfSeriesRecordType">
        <xsd:sequence>
            <xsd:element name="SeriesRecord" type="SeriesRecordType" maxOccurs="unbounded"></xsd:element>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="SeriesRecordType">
        <xsd:sequence>
            <xsd:element name="ServCode" type="xsd:string"></xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

```
You could use an xml parser (like tinyXml: <http://sourceforge.net/projects/tinyxml/>) to parse the document, then simply select the longitude and latitude tags inner text values.