gpt4 book ai didi

java - 简单的 XML 框架 : Having an "inline like" behaviour for objects in ElementMap

转载 作者:数据小太阳 更新时间:2023-10-29 02:41:13 25 4
gpt4 key购买 nike

我正在尝试在 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>

必须有更好的方法来实现,但我无法弄清楚如何,感谢您的帮助

最佳答案

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

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> 的类接口(interface),例如。 EvalListConverter
  2. 设置@Convert()列表类的属性,例如。 @Convert(value = EvalListConverter.class)
  3. 设置AnnotationStrategy坚持: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>

文档:

关于java - 简单的 XML 框架 : Having an "inline like" behaviour for objects in ElementMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com