simplexml 重复元素

发布于 2024-12-18 09:26:21 字数 444 浏览 2 评论 0原文

我有一个如下所示的 xml:

<A>
   <C/>
   <B/>
   <B/>
</A>

在 xml 映射 java 代码中,我有类似这样的内容:

public class A {
  @Element(required=false) 
  private int B;

  @Element(required=false)
  private int C;
  //getters and setters...
}

但我收到如下错误: org.simpleframework.xml.core.PersistenceException:元素“B”在第 1 行声明了两次

我该如何摆脱这个问题?任何人的解决方案都受到高度赞赏。

提前致谢。

i have an xml which looks like this:

<A>
   <C/>
   <B/>
   <B/>
</A>

in the xml mapping java code i have something like this:

public class A {
  @Element(required=false) 
  private int B;

  @Element(required=false)
  private int C;
  //getters and setters...
}

but i am getting an error like this:
org.simpleframework.xml.core.PersistenceException: Element 'B' declared twice at line 1

how do i get rid of this problem? solution from anyone is highly appreciated.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尤怨 2024-12-25 09:26:21

在您的 Xml 中,您有 2 个 B 元素,因此在您的 POJO 中,您需要为 B 提供某种类型的集合(即列表),因为它可以在 XML 中出现 0 次或多次。

In your Xml you have 2 B elements so in your POJO you need to have a collection of some sort (i.e. a List) for B since it can appear in the XML 0 or more times.

忆梦 2024-12-25 09:26:21
public class A {
  @ElementList(inline=true,required=false, entry="B") 
  private List<Integer> B;

  @ElementList(inline=true,required=false, entry="C") 
  private List<Integer> C;
  //getters and setters...
}
public class A {
  @ElementList(inline=true,required=false, entry="B") 
  private List<Integer> B;

  @ElementList(inline=true,required=false, entry="C") 
  private List<Integer> C;
  //getters and setters...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文