moujib
moujib

Reputation: 752

Correct way to parse XML using Simple Framework

I'm using Simple framework to deserialize an xml on my android app .

The issue is on a portion of the xml because I can get the objects of the other portions of it .

Here is the part of the xml that I struggle with :

<webtv name="channelname" date="2014-10-31">
       <config>
              <pathVideo extension="mp4">http://vodflash.channelname.com/tv_conn/1/country/</pathVideo>
              <pathBigImage extension="jpg">http://vodflash.channelname.com/tv_conn/1/country/pic/320x180/</pathBigImage>
              <pathImage extension="jpg">http://vodflash.channelname.com/tv_conn/1/country/pic/160x90/</pathImage>
              <pays>GB;IE</pays>
          </config>

Here is my XmlMapper class :

@Root(name="webtv", strict = false)
public class XmlModelMapper {

    public ConfigObject getConfigObjects() {
        return configObject;
    }

    @Element(name="config")
    public ConfigObject configObject = new ConfigObject();


    @ElementList(name = "itemList")
    private List<Videos> itemList = new ArrayList<Videos>();
    public List<Videos> getItemList() {
        return itemList;
    }

    @ElementList(name = "chaineList")
    private List<Chaine> chaineList = new ArrayList<Chaine>();
    public List<Chaine> getChaineList() {
        return chaineList;
    }
}

If I change my mapper class to this :

@Root(name="webtv", strict = false)
public class XmlModelMapper {

    public List<ConfigObject> getConfigObjects() {
        return configObject;
    }

    //change is here using a list not an object
    @ElementList(name="config")
    public List<ConfigObject> configObjects = new ArrayList<ConfigObject>();


    @ElementList(name = "itemList")
    private List<Videos> itemList = new ArrayList<Videos>();
    public List<Videos> getItemList() {
        return itemList;
    }

    @ElementList(name = "chaineList")
    private List<Chaine> chaineList = new ArrayList<Chaine>();
    public List<Chaine> getChaineList() {
        return chaineList;
    }
}

and the log the size of the List I get 4 which is correct , but how to get each object in a distinct way including the extension (attribute)

Please help me solving this issue .

Thanks

Upvotes: 1

Views: 358

Answers (2)

moujib
moujib

Reputation: 752

Solved my issue and learned alot about simple framework which is great and light weight , but I prefer ollo's answer Please discard ormlite annotations My classes :

@Root(name="webtv", strict = false)
public class XmlModelMapper {

    public ConfigObject getConfigObject() {
        return configObject;
    }

    @Element(name="config")
    public ConfigObject configObject = new ConfigObject();


    //rest of the elements
   ...


}

and

@Root(name = "config" , strict = false)
public class ConfigObject {


    @Element
    PathVideo pathVideo;

    @Element
    PathImage pathImage;

    @Element
    PathBigImage pathBigImage ;


    //rest of the elements
}

and

@Root(name = "pathImage")
@DatabaseTable(tableName = "imageconfig")
public class PathImage {

    @DatabaseField(generatedId = true)
    Integer id ;


    @Attribute
    @DatabaseField
    String extension;

    @Text
    @DatabaseField
    String pathImage;


    //rest of the elements...
}

and

@Root(name = "pathVideo")
@DatabaseTable(tableName = "videoconfig")
public class PathVideo {

    @DatabaseField(generatedId = true)
    Integer id ;

    @Attribute
    @DatabaseField
    String extension;

    @Text
    @DatabaseField
    String pathVideo;


}

and finally

@Root(name = "pathBigImage")
@DatabaseTable(tableName = "bigimageconfig")
public class PathBigImage {


    @DatabaseField(generatedId = true)
    Integer id ;

    @Attribute
    @DatabaseField
    String extension;

    @Text
    @DatabaseField
    String pathBigImage;

  //rest of the elements...
}

Upvotes: 1

ollo
ollo

Reputation: 25350

@ElementList(name="config")
public List<ConfigObject> configObjects = new ArrayList<ConfigObject>();

This wont match with your xml listed above. While the @Element-based solution (example 1) will create a proper config-tag, the list adds another "list-tag"; here's an example:

<config> <!-- This is the list's tag, wrapping all elements -->
    <config> <!-- This is the actual element's tag -->
        <pathVideo extension="mp4">http://video.com</pathVideo>
        <pathBigImage extension="jpg">http://bigimg.com</pathBigImage>
        <pathImage extension="jpg">http://image.com</pathImage>
        <pays>GB;IE</pays>
    </config>

    <!-- some more <config>...</config> -->
</config>

The solution: Use athe inline list - those don't have the "list-tag".

@ElementList(name = "config", inline = true, entry = "config")
public List<ConfigObject> configObjects = new ArrayList<>();

For the sake of completeness, here are my classes used for testing:

class Chaine { /* empty - enough for testing */ }

class Videos { /* empty - enough for testing */ }


@Root
public class ConfigObject
{
    @Element(name = "pathVideo")
    private PathConfig video;
    @Element(name = "pathBigImage")
    private PathConfig bigImage;
    @Element(name = "pathImage")
    private PathConfig image;
    @Element(name = "pays")
    private String pays;

    // ...
}


@Root
public class PathConfig
{
    @Attribute(name = "extension")
    private String extension;
    @Text
    private String path;

    // ...
}

Upvotes: 1

Related Questions