简体   繁体   English

JAXB马歇尔地图 <Integer, ArrayList<String> &gt;

[英]JAXB Marshall Map<Integer, ArrayList<String>>

I have an object i'd like to marshall. 我有一个我想要编组的对象。

@XmlRootElement
public class BoxItem {
  @XmlElement
  Map<Integer, ArrayList<String>> intgerStringArrMap;

  BoxItem() {
      intgerStringArrMap = new HashMap<Integer, ArrayList<String>>();
      for (int i = 0; i < 3; i++) {
          ArrayList<String> stringArrayList = new ArrayList<String>();
          for (int j = 0; j < 10; j++) {
              stringArrayList.add(new BigInteger(130, new SecureRandom()).toString(32));
          }
         intgerStringArrMap.put(i, stringArrayList);
      }
  }
}

Now let's assume we have a boxItem = new BoxItem() 现在让我们假设我们有一个boxItem = new BoxItem()

If i call jaxbMarshaller.marshal(boxItem, System.out);, the values are empty for each entry. 如果我调用jaxbMarshaller.marshal(boxItem,System.out);,则每个条目的值都为空。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<boxItem>
    <intgerStringArrMap>
        <entry>
            <key>0</key>
            <value/>
        </entry>
        <entry>
            <key>1</key>
            <value/>
        </entry>
        <entry>
            <key>2</key>
            <value/>
        </entry>
    </intgerStringArrMap>
</boxItem>

How to marshall the elements inside the ArrayList in a Map value? 如何在Map值中对ArrayList中的元素进行编组?

You should use @XmlElementWrapper Annotation.They are used to produce a wrapper XML element around Collections 您应该使用@XmlElementWrapper Annotation。它们用于围绕Collections生成包装器XML元素

Define a wrapper for your ArrayList like below, ArrayList定义一个包装器,如下所示,

class ListWrapper {

    @XmlElementWrapper(name = "wrapperList")
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
}

Define your Map as below in BoxItem class, BoxItem类中定义如下地图,

@XmlElementWrapper(name = "integerMap")
Map<Integer, ListWrapper> intgerStringArrMap;

Here is the complete class. 这是完整的课程。

@XmlRootElement
public class BoxItem {

    @XmlElementWrapper(name = "integerMap")
    Map<Integer, ListWrapper> intgerStringArrMap;

    BoxItem() {
        intgerStringArrMap = new HashMap<Integer, ListWrapper>();
        for (int i = 0; i < 2; i++) {
            ArrayList<String> stringArrayList = new ArrayList<String>();
            ListWrapper wrapper = new ListWrapper();

            wrapper.setList(stringArrayList);

            for (int j = 0; j < 2; j++) {
                stringArrayList.add("2");
            }
            intgerStringArrMap.put(i, wrapper);
        }
    }

    public static void main(String[] args) throws JAXBException {
        BoxItem box = new BoxItem();
        JAXBContext jc = JAXBContext.newInstance(BoxItem.class);
        jc.createMarshaller().marshal(box, System.out);

    }
}

class ListWrapper {

    @XmlElementWrapper(name = "wrapperList")
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }
}

Running the above should get the below output, 运行以上应该得到以下输出,

<boxItem>
   <integerMap>
      <entry>
         <key>0</key>
         <value>
            <wrapperList>
               <list>2</list>
               <list>2</list>
            </wrapperList>
         </value>
      </entry>
      <entry>
         <key>1</key>
         <value>
            <wrapperList>
               <list>2</list>
               <list>2</list>
            </wrapperList>
         </value>
      </entry>
   </integerMap>
</boxItem>

Jayamohan's answer works, and perhaps is the preferred solution, but if you're ever in a case where you prefer not to modify the RootElement class (BoxItem), you can write your own XmlAdapter so that JAXB knows how to handle cases like Map<Integer, ArrayList<String>> . Jayamohan的答案是有效的,也许是首选的解决方案,但是如果您曾经不想修改RootElement类(B​​oxItem),您可以编写自己的XmlAdapter以便JAXB知道如何处理Map<Integer, ArrayList<String>>

Please refer to How to marshall Map<String, List<Objects>> using JAXB for writing your own XmlAdapter 请参阅如何使用JAXB编写Map <String,List <Objects >>来编写自己的XmlAdapter

如何编组 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.

相关问题 如何马歇尔 HashMap <string, arraylist<string> &gt; 使用 JAXB</string,> - How to Marshall HashMap<String, ArrayList<String>> using 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? 如何编组地图<String, List<Objects> &gt; 使用 JAXB - How to marshall Map<String, List<Objects>> using JAXB JAXB马歇尔清单 <String> JSON? - JAXB marshall List<String> JSON? JAXB注释 - Marshall列表<String []> - JAXB Annotations - Marshall List<String[]> JAXB:如何编组映射到<key>价值</key> - JAXB: how to marshall map into <key>value</key> JAXB @XmlAdapter:Map - &gt; List adapter? (仅限马歇尔) - JAXB @XmlAdapter: Map -> List adapter? (marshall only) JAXB马歇尔字符串转换为base64 - JAXB Marshall String to base64 可以比较来自 Map 的值<integer, arraylist<string> &gt; 与 ArrayList<string></string></integer,> - Can compare values from Map<Integer, ArrayList<String>> with ArrayList<String> 使用字符串Integer映射ArrayList和另一个内部Map - Mapping an ArrayList and another internal Map with String, Integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM