Question

I'm trying to read a config file on Android using Retrofit and SimpleXML.

I have my dependencies set up and a valid XML scheme. Simple XML begins to parse my document but when it reaches a certain tag/depth it fails with the error below

Caused by: retrofit.converter.ConversionException: org.simpleframework.xml.core.ElementException: Element 'Host' does not have a match in class
Caused by: org.simpleframework.xml.core.ElementException: Element 'Host' does not have a match in class

Part of my xml looks like this:

<Config>
    ...
    <Identity>
        <Address>
            <Host>...</Host>
            ...
        </Address>
        ...
     </Identity>
     ...
</Config>

It parses everything up until it reaches the Host tag which is the first element nested that deeply. The error given usually indicates a missing annotation, but the in this case it is annotated as it should be. My mapped object looks like the following:

@Root(name="Config")
public class Config {

    @ElementList(entry="Identity", inline=true)
    private List<Identity> mIdentities;

    @Root(name="Identity")
    static class Identity {

        @Element(name="Address")
        private Address mAddress;

        ...

    }

    @Root(name="Address")
    static class Address {

        @Element(name="Host")
        private String mHost;

        ...
    }
}

Is there anybody else who has experienced a similar issue and has an idea what the problem may be? Or is SimpleXML limited with the depth it can reach? Maybe it's an issue related to ListElement's? I can't find any documentation that mentions any issues.

Was it helpful?

Solution

This exception indicates a missing annotation or a wrong mapping between class and xml. Maybe the nesting is not correct, a wrong annotation is used etc.

It's hard to say whats the cause here, but put make sure your code looks something like this:

(Pseudocode, not testet!)

@Root(name = "Address")
public class Address
{
    @Element(name="Host")
    private String mHost;

    // more things here ...
}

Can you test your problem on a minimal or show at least how address is implemented? Btw. is always helpful to try the opposite direction: Take a Config object, serialize it and check if it's constructed as you require.

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