Simple-Framework: Duplicate annotation (different

2019-07-20 13:18发布

I have an Rss feed that I'd like to parse in Java using Simple Framework. I have problems with 2 elements with the same name, but one of them has a namespace assigned. Here is an example xml:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/">
    <item>
        <title>Regular Titel</title>
        <dc:title>Dc Titel</dc:title>
    </item>
</rss>

Currently my Item.class looks like this:

@Root
public class Item {

    @Namespace(reference = "http://purl.org/dc/elements/1.1/", prefix = "dc")
    @Element(name="title")
    public String dcTitle;

    @Element
    public String title;
}

This obviously causes a PersistenceException (Duplicate annotation of name 'title' on field 'title'....), but i don't really know how I should do this. Could someone please help me to figure this out!

UPDATE

Althought the solution works, I now have problems serializing the objects. The namespaces, that I declare, are not assigned to the elements in the output xml.

2条回答
聊天终结者
2楼-- · 2019-07-20 13:57

Try

@Root
public class Item {

    @Namespace(reference = "http://purl.org/dc/elements/1.1/", prefix = "dc")
    @Path("title[1]")
    @Text
    public String dcTitle;

    @Path("title[2]")
    @Text
    public String title;
}
查看更多
We Are One
3楼-- · 2019-07-20 14:04

Did you try this?

@Root
@Namespace(reference = "http://purl.org/dc/elements/1.1/", prefix = "dc")
public class Item {

    @Element (name = "dc:title")
    public String dcTitle;

    @Element (name = "title")
    public String title;
}
查看更多
登录 后发表回答