Simple-Framework: Duplicate annotation (different namespace)

可紊 提交于 2019-12-24 05:40:11

问题


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.


回答1:


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;
}



回答2:


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;
}


来源:https://stackoverflow.com/questions/16640443/simple-framework-duplicate-annotation-different-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!