问题
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