我正在尝试在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>
必须有更好的方法来实现这一点,但我无法弄清楚如何,谢谢你的帮助
答案 0 :(得分: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()
属性设置转换器。以下是如何操作:
Converter<EvaluationContent>
接口的类,例如。 EvalListConverter
@Convert()
属性设置为列表类,例如。 @Convert(value = EvalListConverter.class)
AnnotationStrategy
设置为persister:Serializer ser = new Persister(new AnnotationStrategy())
另一种方法是实现一个使用Serializer
来编写节点以列出节点的转换器。 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>
<强>文档强>
Converter
) convert
,transform
和strategy