Simple structure: repeating annotation (different namespace)

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

<?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 is as follows:

@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 throws a PersistenceException (duplicating the annotation of the title "title" in the "title" field ....), but I really don't know how to do this. Can someone please help me figure this out!

UPDATE

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

+2
2

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

?

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

Source: https://habr.com/ru/post/1545248/


All Articles