简单的XML架构:具有"内联像"行为在ElementMap对象 [英] Simple XML Framework : Having an "inline like" behaviour for objects in ElementMap
问题描述
我试图序列化Android上的自定义对象的哈希映射获得一个XML这样的:
i am trying to serialize an Hashmap of custom Objects on Android to get an xml like :
<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)
。
之后,内部类:
I created an inner class that contains only the Hashmap that i'm interested in, as i was unable to serialize it the way it is (and read that it's not possible)
added an object to test like this listEval.put(0,currentEvaluation)
.
following, the inner class :
@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对象的定义是这样的:
EvaluationContent object is defined like this :
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;
}
...
}
问题是,我得到一个&LT; evaluationContent&GT;
每个条目的标记:
<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>
必须有一个更好的方式来实现这一点,但我无法弄清楚如何,感谢您的帮助。
There must be a better way to achieve that but i'm unable to figure out how, thanks for your help
推荐答案
我有一个解决方案 - 但不是它并不是完美的:
I have a solution - but not it's not perfect:
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()
属性了。以下是如何做到这一点:
You can set the converter by using @Converter()
attribute too. Here's how to do so:
- 写一个类实现
转换器和LT; EvaluationContent&GT;
接口,例如。EvalListConverter
- 设置
@Convert()
属性的列表类,如:@Convert(值= EvalListConverter.class)
- 设置
AnnotationStrategy
来的持留:串行器SER =新的持留(新AnnotationStrategy())
- Write a class that implements
Converter<EvaluationContent>
interface, eg.EvalListConverter
- Set
@Convert()
Attribute to the list class, eg.@Convert(value = EvalListConverter.class)
- set
AnnotationStrategy
to persister:Serializer ser = new Persister(new AnnotationStrategy())
另一种方法是实现使用一个串行
写的节点列表中的节点的转换器。 Hoewer,你真的要了一下周围玩耍。
Another way is to implement a converter that uses a Serializer
to write the nodes to list nodes. Hoewer, you really have to play around a bit.
有关测试中,我已经把两个值从你的榜样到列表中,并序列化它,生成的XML:
For testing i've put the two values from your example into the list and serialized it, resulting 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>
文件:
- Tutorials / Examples (look for
Converter
there) - API Documentation preliminary packages
convert
,transform
andstrategy
这篇关于简单的XML架构:具有&QUOT;内联像&QUOT;行为在ElementMap对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!