简体   繁体   English

如何在JAXB中编组一个未包装的集合?

[英]How to marshall an unwrapped collection in JAXB?

This is my class: 这是我的班级:

@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
  @XmlElement
  public Collection getElements() {
    List elements = new ArrayList();
    elements.add(new Bar);
    elements.add(new Bar);
    return elements;
  }
}

Class Bar is also simple: Class Bar也很简单:

@XmlType(name = "bar")
@XmlAccessorType(XmlAccessType.NONE)
public static final class Bar {
  @XmlElement
  public String getMessage() {
    return "hello, world!";
  }
}

This is what I'm getting after marshalling of Foo : 这是我整理Foo之后得到的:

<foo>
  <elements xsi:type="foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <message>hello, world!</message>
  </elements>
  <elements xsi:type="foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <message>hello, world!</message>
  </elements>
</foo>

While I'm expecting to get: 当我期望得到:

<foo>
  <bar>
    <message>hello, world!</message>
  </bar>
  <bar>
    <message>hello, world!</message>
  </bar>
</foo>

What should I fix? 我该怎么办?

You will need to annotate the elements property with @XmlElement(name="bar") : 您将需要使用@XmlElement(name="bar")注释elements属性:

@XmlRootElement(name = "foo")
@XmlAccessorType(XmlAccessType.NONE)
public class Foo {
  @XmlElement(name="bar")
  public Collection getElements() {
    List elements = new ArrayList();
    elements.add(new Bar);
    elements.add(new Bar);
    return elements;
  }
}

如何编组 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来编组/取消编组MyBean的集合 - How to use JAXB to marshall/unmarshall a collection of MyBean 如何用jaxb编组? - How to marshall a set with jaxb? JAXB在一个步骤中将XmlElement和XmlAttribute的集合封送处理 - JAXB marshall a Collection to a XmlElement and a XmlAttribute in one step 如何在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 unmarshal unwrapped collections to map with 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?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM