简单框架:重复注释(不同的命名空间) [英] Simple-Framework: Duplicate annotation (different namespace)

查看:22
本文介绍了简单框架:重复注释(不同的命名空间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rss 提要,我想使用 Simple Framework 在 Java 中对其进行解析.我有 2 个同名元素的问题,但其中一个元素分配了命名空间.这是一个示例 xml:

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>

目前我的 Item.class 如下所示:

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

这显然会导致 PersistenceException(字段 'title' 上名称 '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!

更新

虽然解决方案有效,但我现在在序列化对象时遇到了问题.我声明的命名空间没有分配给输出 xml 中的元素.

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.

推荐答案

尝试

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

这篇关于简单框架:重复注释(不同的命名空间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆