3

I need to deserialize an xml response to an object, using simple api. When I deserialize an object, it works just fine, but when I try to deserialize an inline list, I get an exception: org.simpleframework.xml.core.ElementException: Element 'Person' does not have a match in class MyPersons at line 3.

would appreciate a clue on what I am doing wrong or a working example for deserializing an inline list with complex objects.

Thanks.

Attached is a simplified example of my objects:

my xml:

<Persons>
    <Person>
        <Info> 
            <ID>1</ID>
            <Name>A</Name>
        </Info>
        <Address>aaa</Address> 
        <Products> 
            <Product>
                <Name>foo</Name> 
                <Product>foofoo</Product> 
            </Product> 
            <Product>
                <Name>bar</Category>
                <Product>barbar</Product> 
            </Product>
        </Products>
    </Person>
    <Person>
        <Info> 
            <ID>2</ID>
            <Name>B</Name>
        </Info>
        <Address>bbb</Address> 
        <Products> 
            <Product>
                <Name>foo2</Name> 
                <Product>foofoo2</Product> 
            </Product> 
            <Product>
                <Name>bar2</Category>
                <Product>barbar2</Product> 
            </Product>
        </Products>
    </Person>
</Persons>

my objects:

@Root(name="Persons")
public class MyPersons {

    @ElementList(inline=true)
    private List<Person> persons;
}

@Root
public class Person {

    @Element
    private Info Info;

    @Element(required=false)
    private String Address;

    @ElementList
    private List<Product> Products;

    public Person(@Element(name="Info") Info Info){

        this.Info = Info;
        //doing some logic 
    }
}

public class Product {

    @Element
    private String Name; 
    @Element
    private String Product;
}


@Root
public class Info {

    @Element(required=false)
    private String ID;  
    @Element
    private String Name;
}
2
  • As far as I can tell your xml list is actually not inline, since every Person has a proper root element. I would start with removing @ElementList(inline=true). Here are a couple of examples worth looking at: deserializing a list & deserializing a list inline. Commented Jul 19, 2012 at 19:23
  • Thanks. I did see the examples, that is why I thought my list is inline.. if I remove @ElementList(inline=true), how will I get a list of Persons..? Commented Jul 19, 2012 at 20:56

1 Answer 1

5

My apologies for taking such a long time to get back at you. In case you haven't figured out the problem by now, I had a little play with your example and managed to find the solution. It's actually pretty straightforward, and you were really close in getting it to work yourself. Please disregard my earlier comment, because that won't take you any closer to solving the issue.

There are two things you need to change to make it work:

First, add entry="Person" to the element list in MyPersons, such that the result will look like this:

@ElementList(entry="Person", inline=true)
private List<Person> persons;

Second, if you're planning on testing it on the provided xml example, fix up some of the name entries to have a correct closing tag. For example: <Name>bar</Category>, which will obviously break the validity of the xml structure. I'm pretty sure though it's just a typo and that the same error isn't present in your live data.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! works like a charm! :) And yes, it was a typo, these aren't my real objects, I simplified them for the sake of this question..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.