I'm trying to consume XML using SimpleXML by Retrofit 2. After hours of struggling with Kotlin I decided to try Java version and the convert then to Kotlin. And Java version worked well...
error:
java.lang.RuntimeException: org.simpleframework.xml.core.MethodException: Annotation @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=entry, required=true, type=void) must mark a set or get method
I need a Kotlin model class that will be able to consume that XML. Here's input:
<feed>
<entry>
<id> someid </id>
<published> somedate </published>
</entry>
<entry>
<id> someid2 </id>
<published> somedate2 </published>
</entry>
</feed>
Java model class version (works fine):
@Root(name = "feed", strict = false)
public class MFeed {
@ElementList(name = "entry", inline = true)
private List<MEntry> entriesList;
public MFeed(List<MEntry> entriesList) {
this.entriesList = entriesList;
}
public MFeed() {
}
public List<MEntry> getEntriesList() {
return entriesList;
}
public void setEntriesList(List<MEntry> entriesList) {
this.entriesList = entriesList;
}
}
@Root(name = "entry", strict = false)
public class MEntry {
@Element(name = "id")
private String id;
@Element(name = "published")
private String published;
public MEntry() {
}
public MEntry(String id, String published) {
this.id = id;
this.published = published;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPublished() {
return published;
}
public void setPublished(String published) {
this.published = published;
}
}
and autogenerated Kotlin models look like this:
@Root(name = "feed", strict = false)
class Feed {
@ElementList(name = "entry", inline = true)
private var entriesList: List<MEntry>? = null
//autogenerated by converter java -> kotlin
constructor(entriesList: List<MEntry>) {
this.entriesList = entriesList
}
//autogenerated by converter java -> kotlin
constructor() {}
}
@Root(name = "entry", strict = false)
class Entry {
@Element(name = "id")
var id: String? = null
@Element(name = "published")
var published: String? = null
//autogenerated by converter java -> kotlin
constructor() {}
//autogenerated by converter java -> kotlin
constructor(id: String, published: String) {
this.id = id
this.published = published
}
}
I've serfed a lot for consuming XML lists including this post, this and this. None worked. Perhaps they are obsolete. Anyone faced such problem?
Try to add @field
to your annotation @Element*
. And you may move properties to constructor and also you could add data
modifier to your classes. Like this:
@Root(name = "feed", strict = false)
data class Feed(
@field:ElementList(name = "entry", inline = true)
var entriesList: List<Entry>? = null
)
@Root(name = "entry", strict = true)
data class Entry(
@field:Element(name = "id")
var id: String? = null,
@field:Element(name = "published")
var published: String? = null
)
With this I had successfully deserialise xml:
import org.simpleframework.xml.Element
import org.simpleframework.xml.ElementList
import org.simpleframework.xml.Root
import org.simpleframework.xml.core.Persister
private val testXml = """
<feed>
<entry>
<id> someid </id>
<published> somedate </published>
</entry>
<entry>
<id> someid2 </id>
<published> somedate2 </published>
</entry>
</feed>
""".trimIndent()
@Root(name = "feed", strict = false)
data class Feed(
@field:ElementList(name = "entry", inline = true)
var entriesList: List<Entry>? = null
)
@Root(name = "entry", strict = true)
data class Entry(
@field:Element(name = "id")
var id: String? = null,
@field:Element(name = "published")
var published: String? = null
)
fun main(args: Array<String>) {
println(testXml)
val serializer = Persister()
val example = serializer.read(Feed::class.java, testXml)
println(example)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With