マイペースなプログラミング日記

DTMやプログラミングにお熱なd-kamiがマイペースに書くブログ

久しぶりのJAXB

MIDIのノート情報を独自形式で保存したり読み込んだり出来るようにしようと思いJAXBを使ってみることにした。
参考: JAXB With Kotlin – Tech Garage

こんな感じでクラスを作り…

enum class MidiNoteType{ ON, OFF }

@XmlAccessorType(XmlAccessType.FIELD)
class MidiNote(@XmlAttribute var type: MidiNoteType, @XmlElement var number: Int, @XmlElement var position: Int){
    constructor() : this(MidiNoteType.ON, -1, -1)
}

@XmlRootElement(name="notes")
class MidiNoteList(){
    @XmlElement(name="note")
    val notes: MutableList<MidiNote> = mutableListOf()
}

こんな感じで保存処理

File("notes.xml").absoluteFile.bufferedWriter().use{
    JAXB.marshal(sound, it)
}

試しに保存した結果

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<notes>
    <note type="ON">
        <number>36</number>
        <position>250</position>
    </note>
    <note type="ON">
        <number>38</number>
        <position>750</position>
    </note>
</notes>