简体   繁体   English

如何用jaxb编组?

[英]How to marshall a set with jaxb?

I have the following code: 我有以下代码:

@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo
{

   @XmlElement
   private String id;

   ...

}

I would like to be able to marshal a Set<Foo> foos into: 我希望能够将Set<Foo> foos为:

<foos>
   <foo>
       <id>bar1</id>
   </foo>
   <foo>
       <id>bar2</id>
   </foo>
</foos>

Do I need a wrapper class? 我需要包装课吗? If so, how should it look? 如果是这样,它应该看起来如何? Are my annotations correct? 我的注释正确吗? How should the marhalling code look like (if you could illustrate this all, that'd be much appreciated)? 编组代码应如何显示(如果您能说明所有这些,将不胜感激)?

If you want to encapsulate any Collection use XmlElementWrapper 如果要封装任何Collection,请使用XmlElementWrapper

@XmlElementWrapper(name="foos")
@XmlElement(name="foo")
private Set<Foo> foos;

By the way you cannot directly marshall a Set so you have to include your Set inside your own class. 顺便说一下,您不能直接编组Set,因此必须将Set包含在自己的类中。 So if you just want to marshall a set of Foo you have to write a bean like this : 因此,如果您只想编组一组Foo,则必须编写这样的bean:

@XmlRootElement(name = "foos")
public class Foos {
    @XmlElement(name="foo")
    private Set<Foo> foo;
}

如何编组 ArrayList<object> [] 在 JAXB 中?<div id="text_translate"><p> 我有一个 ArrayList[] 类型的变量(列表),我想将它保存在 XML 中。 我尝试了 JAXB,但它只保存了“”字符串(“的重复”等于 list.length)并且在 ArrayLists 中没有任何项目。如果我尝试二维数组,它工作正常。如果我尝试 ArrayList,它也可以正常工作。我怎么解决这个问题?</p><p> 我的代码类似于:</p><pre> @XmlRootElement public class SomeClass { @XmlElement(name="part") private final ArrayList&lt;Object&gt;[] list; ... constructor, which fills the list variable }</pre><p> 有人可以告诉我该怎么做吗? 请。<br><br></p></div></object> - How to marshall ArrayList<Object>[] in JAXB?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JAXB中将XML与名称空间编组? - How to marshall XML with namespace in JAXB? 如何编组 ArrayList<object> [] 在 JAXB 中?<div id="text_translate"><p> 我有一个 ArrayList[] 类型的变量(列表),我想将它保存在 XML 中。 我尝试了 JAXB,但它只保存了“”字符串(“的重复”等于 list.length)并且在 ArrayLists 中没有任何项目。如果我尝试二维数组,它工作正常。如果我尝试 ArrayList,它也可以正常工作。我怎么解决这个问题?</p><p> 我的代码类似于:</p><pre> @XmlRootElement public class SomeClass { @XmlElement(name="part") private final ArrayList&lt;Object&gt;[] list; ... constructor, which fills the list variable }</pre><p> 有人可以告诉我该怎么做吗? 请。<br><br></p></div></object> - How to marshall ArrayList<Object>[] in JAXB? 如何在JAXB中编组一个未包装的集合? - How to marshall an unwrapped collection in JAXB? 如何将JAXB对象编组为不同的模式? - How to marshall JAXB object to a different schema? JAXB:如何编组映射到<key>价值</key> - JAXB: how to marshall map into <key>value</key> JaxB如何将列表的内容编组为单独的标签 - JaxB How to marshall the content of a list as individual tags 如何使用JAXB将列表编组到XML文件? - How to marshall a List to an XML file with JAXB? JAXB XmlAdapter如何用于编组列表? - How can JAXB XmlAdapter be used to marshall lists? 如何使用JAXB来编组/取消编组MyBean的集合 - How to use JAXB to marshall/unmarshall a collection of MyBean jaxb如何以某种顺序编组值? - jaxb how to marshall values in some order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM