13

I am using the org.simpleframework.xml to handle some xml tasks for an android application and am running into the following error which I cannot figure out.

org.simpleframework.xml.core.PersistenceException: Element 'album' is already used with @org.simpleframework.xml.ElementList(inline=false, name=album, entry=, data=false, empty=true, required=true, type=void) on field 'albums' private java.util.List us.blah.sonar.xsd.simple.AlbumList.albums at line 5
at org.simpleframework.xml.core.Variable$Adapter.read(Variable.java:484)
at org.simpleframework.xml.core.Composite.readVariable(Composite.java:613)
at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)

Here is a sample of the XML I'm trying to unmarshall:

<albumList> 
    <album id="3985" parent="3301" title="Bob Dylan - The Reissue Series Sampler" album="Bob Dylan - The Reissue Series Sampler" artist="48 - Masked and Anonymous [BSO] (2003)" isDir="true" created="2011-12-09T11:52:12"/> 
    <album id="3986" parent="3301" title="Soundtrack - Masked & Anonymous" album="Soundtrack - Masked & Anonymous" artist="48 - Masked and Anonymous [BSO] (2003)" isDir="true" coverArt="3986" created="2011-12-09T11:52:13"/> 
    <album id="10542" parent="145" title="A Perfect Circle" album="Acoustica" artist="A Perfect Circle" isDir="true" created="2011-12-09T12:01:52"/> 
</albumList>

And here are the POJOs I created and annotated:

@Root
public class AlbumList {

    @ElementList(name="album")
    private List<Child> albums;

    public List<Child> getAlbums() {
        return albums;
    }

}

public class Child {
    @Attribute
    private String id;

    @Attribute(required=false)
    private String parent;

    @Attribute
    private boolean isDir;

    @Attribute
    private String title;

    @Attribute(required=false)
    private String album;

    @Attribute(required=false)
    private String artist;

    @Attribute(required=false)
    private int track;

    @Attribute(required=false)
    private int year;

    @Attribute(required=false)
    private String genre;

    @Attribute(required=false)
    private String coverArt;

    @Attribute(required=false)
    private long size;

    @Attribute(required=false)
    private String contentType;

    @Attribute(required=false)
    private String suffix;

    @Attribute(required=false)
    private String transcodedContentType;

    @Attribute(required=false)
    private String transcodedSuffix;

    @Attribute(required=false)
    private int duration;

    @Attribute(required=false)
    private int bitRate;

    @Attribute(required=false)
    private String path;

    @Attribute(required=false)
    private boolean isVideo;

    @Attribute(required=false)
    private String userRating;

    @Attribute(required=false)
    private double averageRating;

    @Attribute(required=false)
    private int discNumber;

    @Attribute(required=false)
    private Date created;

    @Attribute(required=false)
    private Date starred;

    @Attribute(required=false)
    private String albumId;

    @Attribute(required=false)
    private String artistId;

    @Attribute(required=false)
    private MediaType type;
}

Can anyone lead me in the right direction on how I can fix the issue, or what exactly this error message means? Is it because the XML element is named album and there is an attribute also named album?

ollo
  • 24,797
  • 14
  • 106
  • 155
cjackson
  • 1,637
  • 1
  • 13
  • 25

1 Answers1

31

Try to annotate your AlbumList class this way:

@Root
public class AlbumList {
    @ElementList(entry="album", inline=true)
    private List<Child> albums;

    public List<Child> getAlbums() {
        return albums;
    }
}
Vladimir Mironov
  • 30,514
  • 3
  • 65
  • 62
  • 2
    That worked. Thank you very much. I'm not sure why that works though. I didn't think the album elements were inline since they are wrapped with the albumList tags. – cjackson Jan 23 '13 at 16:17
  • If this answer solved your problem you may [accept](http://stackoverflow.com/faq#howtoask) it. – ollo Jan 23 '13 at 17:29
  • 1
    I wish that the default was inline=true... I just hit this problem. Thanks for the solution. – Neal Sanche Sep 04 '13 at 18:43
  • Any chance what I should do if it's not a list? For example, if the backend is incorrect and has two nodes with the same name, how do I avoid this persistenceexception? – AdamMc331 Jan 02 '17 at 02:21
  • @McAdam331 you should post a new question for that. – Jorge E. Hernández Mar 23 '17 at 17:29