gpt4 book ai didi

java - 您如何使用简单的自定义转换器访问字段注释?

转载 作者:行者123 更新时间:2023-11-30 11:47:22 25 4
gpt4 key购买 nike

我正在使用 Simple (http://simple.sourceforge.net/) 库在 Java 中编码/解码 XML 数据。对于我的一些更复杂的数据结构,我需要编写自己的转换器。例如,假设我有一个 List<List<String>>我需要编码。我写了以下内容:

class WorldObject {

@Element(name="vector-names")
@Convert(ListListConverter.class)
private List<List<String>> vectorNames;

/** Constructor and other details ... **/

}

连同 ListListConverter(我暂时忽略了解码器):

class ListListConverter implements Converter<List<List<String>>> {

@Override
public List<List<String>> read(InputNode node) throws Exception {
// stub
return null;
}

@Override
public void write(OutputNode node, List<List<String>> value)
throws Exception {

node.setName("list-list-string");

for (List<String> list : value) {
OutputNode subList = node.getChild("list-string");

for (String str : list) {
OutputNode stringNode = subList.getChild("string");
stringNode.setValue(str);
}

subList.commit();
}

node.commit();

}

}

此设置工作正常,并生成我想要的 XML。但是,我想访问 @Element注释的 name字段,以便我可以为标签指定指定名称(在本例中为 "vector-names"),而不是默认名称("list-list-string")。这就是 Simple 开箱即用地处理所有类型的编码方式,因此必须有一种方法可以从自定义转换器访问该数据。

我怎样才能做到这一点?

最佳答案

不能以这种方式获取注释,因为无法通过字段-转换器中的字段访问它。
解决方案是编写一个WorldObject-Converter - 即使您只想编写一个字段。

WorldObject 类:

@Root
@Convert(WorldObjectConverter.class) // specify converter for this class
public class WorldObject
{
@Element(name = "vector-names")
private List<List<String>> vectorNames;

// only for the example below - write whatever constructor(s) you need
public WorldObject()
{
this.vectorNames = new ArrayList<>();
}

// constructor, getter / setter, etc.


// a getter is required to access the field in the converter.
public List<List<String>> getVectorNames()
{
return vectorNames;
}
}

WorldObjectConverter 类:

public class WorldObjectConverter implements Converter<WorldObject>
{
@Override
public WorldObject read(InputNode node) throws Exception
{
throw new UnsupportedOperationException("Not supported yet.");
}


@Override
public void write(OutputNode node, WorldObject value) throws Exception
{
final Field f = value.getClass().getDeclaredField("vectorNames"); // get the field 'vectorNames' of the 'WorldObject' class
final Element elementAnnotation = f.getAnnotation(Element.class); // get the 'Element' annotation of the Field

final String name = elementAnnotation.name(); // get the 'name'-value of the annotation
node.setName(name); // set Nodename


for( List<String> list : value.getVectorNames() )
{
OutputNode subList = node.getChild("list-string");

for( String str : list )
{
OutputNode stringNode = subList.getChild("string");
stringNode.setValue(str);
}

subList.commit();
}

node.commit();
}
}

示例:

final File f = new File("test.xml"); // output file

WorldObject wo = new WorldObject(); // the object to serialize

// some testdata ...
List<String> l = new ArrayList<>();
l.add("a");
l.add("b");
wo.getVectorNames().add(l);

l = new ArrayList<>();
l.add("c");
l.add("d");
wo.getVectorNames().add(l);


// create the serializer - dont forget the AnnotationStrategy!
Serializer ser = new Persister(new AnnotationStrategy());
ser.write(wo, f); // serialize it to file

输出:

<vector-names>
<list-string>
<string>a</string>
<string>b</string>
</list-string>
<list-string>
<string>c</string>
<string>d</string>
</list-string>
</vector-names>

完成!

关于java - 您如何使用简单的自定义转换器访问字段注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9343348/

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