title: Codable and XMLParser in Swift
body: <p>Using the new Swift4 <code>Codable</code> protocol works well for JSON-decoding (as explained <a href="https://grokswift.com/json-swift-4/" rel="nofollow noreferrer">here</a> or <a href="https://benscheirman.com/2017/06/swift-json/" rel="nofollow noreferrer">here</a> or in many other contributions). However, as it comes to XML-parsing, I couldn't find any information on whether this <code>Codable</code> protocol could also be used for XML decoding.</p>
  <p>I tried to use the XMLParser (as can be seen in code excerpts below). But I failed to used the &quot;Codable&quot; protocol to simplify the XML-parsing process. How would I have to use the <code>Codable</code> protocol exactly to simplify XML-parsing ??</p>
  <pre class="lang-swift prettyprint-override"><code>// the Fetching of the XML-data (excerpt shown here with a simple dataTask) :
  
  let myTask = session.dataTask(with: myRequest) { (data, response, error) in
              
      // check for error
      guard error == nil else {
          completionHandler(nil, error!)
          return
      }
      // make sure we got data in the response
      guard let responseData = data else {
         let error = XMLFetchError.objectSerialization(reason: &quot;No data in response&quot;)
          completionHandler(nil, error)
          return
      }
              
      // the responseData is XML !!!!!!!!!!!!!!
      let parser = XMLParser(data: responseData)
      parser.delegate = self
      parser.parse()
  }
  myTask.resume()
  </code></pre>
  <p>The corresponding XMLParserDelegate-methods:</p>
  <pre><code>func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
      
      self.resultTrip = elementName
      
      // print(elementName)
      if (self.resultTrip == &quot;TripResult&quot;) {
          self.resultTime = &quot;&quot;
      }
      
  }
  
  func parser(_ parser: XMLParser, foundCharacters string: String) {
      
      let data = string.trimmingCharacters(in: .whitespacesAndNewlines)
      
      if data.count != 0 {
          
          switch self.resultTrip {
          case &quot;TimetabledTime&quot;: self.resultTime = data
          default: break
          }
      }
  }
  
  func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
      
      if self.resultTrip == &quot;TripResult&quot; {
          
          // HERE IS THE MISSING BIT: HOW DO YOU USE A CODABLE struct ???
          var myTrip = TripResult(from: &lt;#Decoder#&gt;)
          myTrip.resultID = self.resultTrip
          
      }
      
      print(resultTime)
  }
  </code></pre>
  <p>The struct :</p>
  <pre><code>struct TripResult : Codable {
      let resultId : String?
      let trip : Trip?
  
      enum CodingKeys: String, CodingKey {
  
          case resultId = &quot;ResultId&quot;
          case trip
      }
  
      init(from decoder: Decoder) throws {
          let values = try decoder.container(keyedBy: CodingKeys.self)
          resultId = try values.decodeIfPresent(String.self, forKey: .resultId)
          trip = try Trip(from: decoder)
      }
  }
  </code></pre>
  <p>How would I have to use the <code>Codable</code> struct? Is there any nice example of how to use the <code>Codable</code> protocol for XML parsing?</p>