Java 简单的XML框架:有一个;内联式;ElementMap中对象的行为
我正在尝试序列化Android上自定义对象的Hashmap,以获得如下xml:Java 简单的XML框架:有一个;内联式;ElementMap中对象的行为,java,android,xml,xml-serialization,simple-framework,Java,Android,Xml,Xml Serialization,Simple Framework,我正在尝试序列化Android上自定义对象的Hashmap,以获得如下xml: <ROWSET> <ROW num="0"> <Name>foo</Name> <FNAME>bar</FNAME> <BIRTH>01/01/2000</BIRTH> <Num>4376484</NUM> </ROW>
<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>
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;
}
...
}
问题是,每个条目都有一个
标记:
<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>
福
酒吧
01/01/2000
4376484
...
必须有更好的方法来实现这一点,但我不知道如何实现,感谢您的帮助我有一个解决方案-但它并不完美:
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
文档:
- /(在那里查找转换器)
- 初步包
,转换
和转换
策略
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
<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>