3

我正在尝试在 Android 上序列化自定义对象的 Hashmap 以获得类似的 xml:

<ROWSET>
   <ROW num="0">
      <Name>foo</Name>
      <FNAME>bar</FNAME>
      <BIRTH>01/01/2000</BIRTH>
      <Num>4376484</NUM>
   </ROW>
   <ROW num="1">
      <Name>bar</Name>
      <FNAME>foo</FNAME>
      <BIRTH>02/02/2000</BIRTH>
      <NUM>4376484</NUM>
   </ROW>
</ROWSET>

我创建了一个仅包含我感兴趣的 Hashmap 的内部类,因为我无法按原样序列化它(并且读到它是不可能的)添加了一个对象来像这样进行测试listEval.put(0,currentEvaluation)。下面是内部类:

@Root (name="ROWSET")
public static class listOfEvals {

    @ElementMap (entry="ROW", key="num", attribute=true, inline=true)
    private Map<Integer, EvaluationContent> evalList;

    public listOfEvals(Map<Integer, EvaluationContent> list){
        evalList=list;
    }

    public Map<Integer, EvaluationContent> getEvalList() {
        return evalList;
    }

    public void setEvalList(Map<Integer, EvaluationContent> evalList) {
        this.evalList = evalList;
    }
}

EvaluationContent 对象的定义如下:

public class EvaluationContent {

    @Element(name="Name", required = false)
    private String mName; 
    @Element(name="FNAME", required = false)         
    private String mFname;
    @Element(name="BIRTH", required = false)        
    private String mBirth; 
    @Element(name="Num", required = false)        
    private String mNum; 

    public String getName() {
        return mName;
    }
    public void setName(String mName) {
        this.mName = mName;
    }
     ... 
    }

问题是我<evaluationContent>为每个条目获取了一个标签:

<ROWSET>
       <ROW num="0">
          <evaluationContent>
            <Name>foo</Name>
            <FNAME>bar</FNAME>
            <BIRTH>01/01/2000</BIRTH>
            <Num>4376484</NUM>
          </evaluationContent>
       </ROW>
       <ROW num="1">
          <evaluationContent>
         ...
          <evaluationContent>
       </ROW>
    </ROWSET>

必须有更好的方法来实现这一点,但我无法弄清楚如何,谢谢你的帮助

4

1 回答 1

1

我有一个解决方案 - 但它并不完美:

Registry registry = new Registry();

// Bind the list's class to it's converter. You also can implement it as a "normal" class.
registry.bind(EvaluationContent.ListOfEvals.class, new Converter<EvaluationContent.ListOfEvals>()
{
    @Override
    public EvaluationContent.ListOfEvals read(InputNode node) throws Exception
    {
        /* Implement if required */
        throw new UnsupportedOperationException("Not supported yet.");
    }


    @Override
    public void write(OutputNode node, EvaluationContent.ListOfEvals value) throws Exception
    {
        Iterator<Map.Entry<Integer, EvaluationContent>> itr = value.getEvalList().entrySet().iterator();

        while( itr.hasNext() )
        {
            final Entry<Integer, EvaluationContent> entry = itr.next();
            final EvaluationContent content = entry.getValue();

            // Here's the ugly part: creating the full node
            final OutputNode child = node.getChild("ROW");

            child.setAttribute("num", entry.getKey().toString());
            child.getChild("Name").setValue(content.getName());
            child.getChild("FNAME").setValue(content.getFName());
            child.getChild("BIRTH").setValue(content.getBirth());
            child.getChild("Num").setValue(content.getNum());
        }   
    }
});

Strategy strategy = new RegistryStrategy(registry);
Serializer ser = new Persister(strategy);
ser.write(list, f); // f is the Output (eg. a file) where you write to

您也可以使用@Converter()属性设置转换器。这样做的方法如下:

  1. 编写一个实现Converter<EvaluationContent>接口的类,例如。EvalListConverter
  2. @Convert()属性设置为列表类,例如。@Convert(value = EvalListConverter.class)
  3. 设置AnnotationStrategy为持久性:Serializer ser = new Persister(new AnnotationStrategy())

另一种方法是实现一个转换器,该转换器使用 aSerializer将节点写入列表节点。Hoewer,你真的必须玩一下。

为了进行测试,我已将示例中的两个值放入列表中并对其进行序列化,生成 Xml:

<ROWSET>
   <ROW num="0">
      <Name>foo</Name>
      <FNAME>bar</FNAME>
      <BIRTH>01/01/2000</BIRTH>
      <Num>4376484</Num>
   </ROW>
   <ROW num="1">
      <Name>foo</Name>
      <FNAME>bar</FNAME>
      <BIRTH>02/02/2000</BIRTH>
      <Num>4376484</Num>
   </ROW>
</ROWSET>

文档:

于 2013-06-19T21:50:18.827 回答