Question

I'm working on a Android project, which is using an API to get it's data. Now first of all, I can't change anything to the API because it's also used in an iPhone app which has allready been launced. So i have to work around this.

I'm trying to read the XML from the API using XStream. All is going well and XStream is working nice and easy. Until I stumbled upon an API call with ambigious tags. The XML That was returned by the API was as following:

<response>
    <plant>
        <Name />
        <Description />
        <KeyValues>
            <entry>
                <Key />
                <Value />
            </entry>
            <entry>
                <Key />
                <Value />
            </entry>
            <entry>
                <Key />
                <Value />
            </entry>
        </KeyValues>
        <Tasks>
            <entry>
                <Title />
                <Text />
            </entry>
            <entry>
                <Title />
                <Text />
            </entry>
        </Tasks>
    </plant>
</response>

As you can see both the tag KeyValues as the tag Tasks contain entry tags. The problem i'm having is that i can't specifically alias the entry tag to a java class i have. My plant class is looking as following:

public class Plant extends BaseModel {
    private String Name;
    private String Description;

    private List<KeyValue> KeyValues;
    private List<Task> Tasks;
}

Where the KeyValue and Task classes are essentially the two entry classes. But when i try to deserialize the xml I get the following error:

com.thoughtworks.xstream.converters.ConversionException: Cannot construct java.util.Map$Entry as it does not have a no-args constructor : Cannot construct java.util.Map$Entry as it does not have a no-args constructor
---- Debugging information ----
message             : Cannot construct java.util.Map$Entry as it does not have a no-args constructor
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot construct java.util.Map$Entry as it does not have a no-args constructor
class               : java.util.Map$Entry
required-type       : java.util.Map$Entry
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
ath                : /response/plant/KeyValues/entry
line number         : 1
class[1]            : java.util.ArrayList
converter-type[1]   : com.thoughtworks.xstream.converters.collections.CollectionConverter
class[2]            : com.example.android.stadseboeren.model.Plant
version             : 0.0
-------------------------------

I get the fact that using ambiguous tags in an xml is not an ideal situation but there is nothing i can do to change it now.

Is there anybody who can help me solve this problem?

Cheers Daan

Was it helpful?

Solution

OK, so to be a good citizen i'll post the answer here because i figured it out.

Eventually i ended up creating a extra class which essentially is just a holder for the entry list.

public class KeyValues extends BaseModel {

    @XStreamImplicit(itemFieldName="entry")
    private ArrayList<KeyValueEntry> entries;
}

Using the XStreamImplicit i can bind the entry object to my arraylist.

It's not the most pretty solution but it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top