【问题标题】:How to unmarshal array or list using JAXB?如何使用 JAXB 解组数组或列表?
【发布时间】:2022-01-05 06:03:04
【问题描述】:

我有一个 XML 文件,我需要用我的 POJO 类设置它

<ids xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" >
  <a:string>100</a:string>
  <a:string>101</a:string>
  <a:string>102</a:string>
  ... etc..
</ids>

我必须使用哪个注释来获取这些值

我正在使用以下方式。

@XmlElement(name="string",namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays")
    protected List<String> id;

但我得到了 null

【问题讨论】:

  • 整个 XML 所以根元素是&lt;ids&gt;

标签: java xml jaxb unmarshalling


【解决方案1】:

您没有展示包含protected List&lt;String&gt; id 的类。应该是这样的

@XmlRootElement(name = "ids")
public class Wrapper {
    @XmlElement(name = "string",
                namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")
    protected List<String> id;
}

填充列表。您也可以将类命名为 Ids 并删除 name = 'Ids'

【讨论】:

    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    相关资源
    最近更新 更多