Parsing XML using Simple XML Framework in JAva

468 views Asked by At

I am trying to parse some XML using the Simple XML framework, however I'm having trouble with getting it work. This is the model I used:

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "rootelement", strict = false)
public class XMLModel {
    @Element(name = "tag")
    private TagModel tag;

    @Element(name = "id", required = false)
    private String id;

    @Element(name = "url", required = false)
    private String url;

    public TagModel getTag() {
        return tag;
    }

    public void setTag(TagModel tag) {
        this.tag = tag;
    }

    public String getId() {
        return id;
    }

    public void setId(String videoId) {
        this.id = id;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

And the TagModel file is:

import org.simpleframework.xml.Attribute;

public class TagModel implements Parcelable {
    @Attribute(name = "data", required = false)
    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

Here's the XML I'm trying to parse.

<?xml version="1.0" encoding="UTF-8"?>
<rootelement version="1.0">
    <tag data="none"/>
    <id><![CDATA[95757410]]></id>
    <url><![CDATA[http://google.com]]></url>
</rootelement>

I get the following error with the current code:

retrofit.RetrofitError: org.simpleframework.xml.core.PersistenceException: Constructor not matched for class

If anyone would know how I could get the various data (including the TagModel) I'd really appreciate it!

Thanks for the help, Daniel

1

There are 1 answers

0
DVassilev On BEST ANSWER

I realised the error was due to not having a constructor in the models.

For example, in TagModel I needed to add the following:

public TagModel(@Attribute(name = "data") String data) {
    this.data = data;
}