Issue with MusicXML file
frnic opened this issue · comments
I am new to XML decoding and have a issue decoding this part of a musicXML file with your decoder.
Issues:
(Item 1 I figured out what I was doing wrong.)
// 1. Attributes on measure element (number="4" width="267.83") are missing - or I don't understand
// how to setup the structure to return them - using the XMLParser the attributes show up.
2. <rest/> doesn't get detected/decoded - or I don't know how to differentiate between this and a missing "rest" element since both show in my structure as nil.
(grr - item 3 was a typo on me.)
//3. barline element bar-style is not decoded and/or returns nil and the barline attribute (direction) is //not decoded or I do not know how to setup my structure.
Here is a print of that part of the score struct:
xmlTest.Note(rest: nil, pitch: nil, duration: Optional(1), voice: Optional(1), type: Optional("quarter"), stem: nil)]), barlne: nil)
(fragment of musicXML file)
<measure number="4" width="267.83">
<note>
<rest/>
<duration>1</duration>
<voice>1</voice>
<type>quarter</type>
</note>
<barline location="right">
<bar-style>light-heavy</bar-style>
<repeat direction="backward"/>
</barline>
</measure>
...
(structs to receive decoded data)
struct Measure: Codable {
var number: Int?
var width: Float?
var attributes: Attributes?
var note: [Note]?
var barline: Barline?
}
struct Attributes: Codable {
var divisions: Int?
var key: Key?
var time: Time?
var clef: Clef?
}
struct Note: Codable {
var rest: String?
var pitch: Pitch?
var duration: Int?
var voice: Int?
var type: String?
var stem: String?
}
struct Barline: Codable {
var barStyle: String?
var repeatType: String?
enum CodingKeys: String, CodingKey {
case barStyle = "bar-style"
case repeatType = "repeat"
}
}
And here is the code fragment for the decoding:
let decoder = XMLDecoder()
let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
decoder.dateDecodingStrategy = .formatted(formatter)
do {
let score = try decoder.decode(Score.self, from: data)
print("Score: \(score)")
} catch {
print(error)
}