iOS Development

ios – XML similar merchandise with completely different tags decoded into similar bucket

Spread the love


That is the XML file I wish to decode

<workout_file>
    <writer>afaag</writer>
    <title>afa</title>
    <description>ag326 wges</description>
    <sportType>bike</sportType>
    <tags>
    </tags>
    <exercise>
        <Warmup Period="600" PowerLow="0.25" PowerHigh="0.75"/>
        <Cooldown Period="600" PowerLow="0.75" PowerHigh="0.25"/>
    </exercise>
</workout_file>

That is the code which I am presently utilizing to decode the XML. I initially thought that the code will correctly differentiate between the XMLs based mostly on the tags <Warmup> <Cooldown> and so forth but it surely looks like it is utilizing the attributes inside the tags.

Is there one thing that may be achieved to correctly decode it based mostly on the tags? Observe that I’m utilizing XMLCoder (https://github.com/CoreOffice/XMLCoder)

struct Exercise: Codable {
  let exercise: [WorkoutSegment]
  
  enum CodingKeys: String, CodingKey {
    case exercise = "exercise"
  }

  init(from decoder: Decoder) throws {
    let container = strive decoder.container(keyedBy: CodingKeys.self)
    var workoutSegments = [WorkoutSegment]()
    var workoutContainer = strive container.nestedUnkeyedContainer(forKey: .exercise)
    
    whereas !workoutContainer.isAtEnd {

     if let warmUp = strive? workoutContainer.decode(WarmUp.self) {
        print("decoding WarmUp")
        workoutSegments.append(WorkoutSegment(warmUp: warmUp))
      } else if let coolDown = strive? workoutContainer.decode(CoolDown.self) {
        print("decoding CoolDown")
        workoutSegments.append(WorkoutSegment(coolDown: coolDown))
      } else {
        // If the subsequent aspect can't be decoded, exit the loop
        print("=== oops ===")
        break
      }
    }
    
    self.exercise = workoutSegments
  }
}


struct WorkoutSegment: Codable {
  let warmUp: WarmUp?
  let coolDown: CoolDown?
  
  enum CodingKeys: String, CodingKey {
    case warmUp = "Warmup"
    case coolDown = "Cooldown"
  }
  
  init(warmUp: WarmUp? = nil, coolDown: CoolDown? = nil) {
    self.warmUp = warmUp
    self.coolDown = coolDown
  }
}


// <Warmup Period="300" PowerLow="0.42449999" PowerHigh="0.70449996" />
struct WarmUp: Codable {
  let period: Double
  let powerLow: Double
  let powerHigh: Double
  let textevent: [TextEvent]?
  
  enum CodingKeys: String, CodingKey {
    case period = "Period"
    case powerLow = "PowerLow"
    case powerHigh = "PowerHigh"
    case textevent = "textevent"
  }
}

struct CoolDown: Codable {
  let period: Double
  let powerLow: Double
  let powerHigh: Double
  let textevent: [TextEvent]?
  
  enum CodingKeys: String, CodingKey {
    case period = "Period"
    case powerLow = "PowerLow"
    case powerHigh = "PowerHigh"
    case textevent = "textevent"
  }
}

One thought on “ios – XML similar merchandise with completely different tags decoded into similar bucket

  1. Wow amazing blog layout How long have you been blogging for you made blogging look easy The overall look of your web site is magnificent as well as the content

Leave a Reply

Your email address will not be published. Required fields are marked *