title: Parsing XML file in Swift (Xcode v 7.0.1) and retrieving values from dictionary
body: <p>I'm trying to parse an XML file in Swift and I'm having a little bit of trouble understanding how the NSMutableDictionary works in storing values from elements.</p>
  
  <p>The XML file looks like this,</p>
  
  <pre><code>&lt;coord2 count="6"&gt;
    &lt;markers&gt;
      &lt;marker&gt;
        &lt;lat&gt;36.99058167&lt;/lat&gt;
        &lt;lng&gt;-122.06620333&lt;/lng&gt;
        &lt;timestamp&gt;1444931620&lt;/timestamp&gt;
        &lt;route&gt;LOOP&lt;/route&gt;
        &lt;id&gt;7855&lt;/id&gt;
        &lt;predictions&gt;
        ,4,5,,6,7,,,,10,,,11,,11,,13,15,,14,,,16,,,0,,,2,,,,,,,,,,,,,,
        &lt;/predictions&gt;
        &lt;update_seconds&gt;10&lt;/update_seconds&gt;
        &lt;index&gt;4&lt;/index&gt;
      &lt;/marker&gt;
      &lt;marker&gt;
        &lt;lat&gt;36.99296&lt;/lat&gt;
        &lt;lng&gt;-122.06517333&lt;/lng&gt;
        &lt;timestamp&gt;1444934786&lt;/timestamp&gt;
        &lt;route&gt;UPPER CAMPUS&lt;/route&gt;
        &lt;id&gt;7860&lt;/id&gt;
        &lt;predictions&gt;
        15,,,14,,,13,12,11,,10,9,,8,,7,,,6,,5,4,,3,2,,1,0,,,,,,,,,,,,,,,
        &lt;/predictions&gt;
        &lt;update_seconds&gt;10&lt;/update_seconds&gt;
        &lt;index&gt;4&lt;/index&gt;
      &lt;/marker&gt;
    &lt;/markers&gt;
    &lt;curr_time&gt;1444931622&lt;/curr_time&gt;
  &lt;/coord2&gt;
  </code></pre>
  
  <p>The way I'm attempting to parse the XML file is with the following code that I found from a tutorial at 
  <a href="http://www.theappguruz.com/blog/xml-parsing-using-nsxmlparse-swift" rel="nofollow">http://www.theappguruz.com/blog/xml-parsing-using-nsxmlparse-swift</a></p>
  
  <pre><code>import UIKit
  import GoogleMaps
  
  class ViewController: UIViewController, NSXMLParserDelegate {
  
      var parser = NSXMLParser()
      var posts = NSMutableArray()
      var elements = NSMutableDictionary()
      var element = NSString()
      var route = NSMutableString()
      var timestamp = NSMutableString()
  
      func beginParsing()
      {
          posts = []
          parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://xmfile.xml"))!)!
          parser.delegate = self
          parser.parse()
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          beginParsing()
  
          print("The route is")
          let t = elements["route"]!
          print(t)
          print("The timestamp is")
          let u = elements["timestamp"]!
          print(u)
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      // didStartElement
      func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
      {
          element = elementName
          if (elementName as NSString).isEqualToString("marker")
          {
              elements = NSMutableDictionary()
              elements = [:]
              route = NSMutableString()
              route = ""
              timestamp = NSMutableString()
              timestamp = ""
          }
      }
  
      // foundCharacters
      func parser(parser: NSXMLParser, foundCharacters string: String)
      {
          if element.isEqualToString("route") {
              route.appendString(string)
          } else if element.isEqualToString("timestamp") {
              timestamp.appendString(string)
          }
      }
  
      // didEndElement
      func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
      {
          if (elementName as NSString).isEqualToString("marker") {
              if !route.isEqual(nil) {
                  elements.setObject(route, forKey: "route")
              }
              if !timestamp.isEqual(nil) {
                  elements.setObject(timestamp, forKey: "timestamp")
              }
              posts.addObject(elements)
          }
      }
  
  }
  </code></pre>
  
  <p>Right now I'm only focusing on the "route" and "timestamp" elements just to gain an understanding of how parsing works. The output I get when I</p>
  
  <pre><code>let t = elements["route"]!
  print(t)
  print("previous is route")
  let u = elements["timestamp"]!
  print(u)
  print("previous is timestamp")
  </code></pre>
  
  <p>is always the value of the <strong>last</strong> elements that have those names in the XML file. So, for the example XML file I provided the output of the 6 lines of code above would be</p>
  
  <p><strong>The route is</strong><br>
  <strong>UPPER CAMPUS</strong></p>
  
  <p><strong>The timestamp is</strong><br>
  <strong>1444934786</strong></p>
  
  <p>I want to be able to distinguish between the multiple "<strong>marker</strong>" elements in the XML file and retrieve the values of elements within marker. In other words, be able to get the values of elements within the first marker also, not just the last one. How would I go about doing this?</p>