更多信息(代码、预期的 XML 等)会有所帮助......
但这里有一个例子,你可以如何做到这一点:
关键特性是实现Converter
,您可以在其中自定义对象的序列化/反序列化方式。在下面的代码中,我Converter
为Child
该类实现了一个,但是也可以为Parent
该类实现它。
Child
班级:
@Root(name = "child")
@Convert(value = ChildConverter.class) // Set the Converter
public class Child
{
private boolean actived;
@Element(name = "value", required = true)
private String value;
@Element(name = "value2", required = true)
private int secondValue;
public Child(boolean actived, String value, int secondValue)
{
this.actived = actived;
this.value = value;
this.secondValue = secondValue;
}
public boolean isActived()
{
return actived;
}
public String getValue()
{
return value;
}
public int getSecondValue()
{
return secondValue;
}
// ...
}
除了activated
标志之外,这个类还有另外两个成员来展示如何序列化它们。
Parent
班级:
@Root(name = "parent")
public class Parent
{
@ElementList(name = "childs", required = true)
private List<Child> childs;
public Parent()
{
this.childs = new ArrayList<>();
}
public void addChild(Child child)
{
childs.add(child);
}
// ...
}
Converter
实施:
public class ChildConverter implements Converter<Child>
{
@Override
public Child read(InputNode node) throws Exception
{
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void write(OutputNode node, Child value) throws Exception
{
if( value.isActived() == true ) // Check if 'activated' flag is set
{
// Set valus of the child
node.setValue(value.getValue());
node.setAttribute("secondValue", String.valueOf(value.getSecondValue()));
}
else
{
node.remove(); // Remove the node since we don't need it
}
}
}
到目前为止,实现并不是很复杂。首先我们检查是否activated
设置。如果是,我们将对象值填充到节点中,如果未设置,我们删除节点(否则您将<child />
在 XML 中获得 a)。
如何使用:
// Some test data
Parent p = new Parent();
p.addChild(new Child(true, "a", 1));
p.addChild(new Child(true, "b", 2));
p.addChild(new Child(false, "c", 3)); // "disabled"
p.addChild(new Child(true, "d", 4));
p.addChild(new Child(false, "e", 5)); // "disabled"
p.addChild(new Child(false, "f", 6)); // "disabled"
final File f = new File("test.xml");
Serializer ser = new Persister(new AnnotationStrategy()); // Don't forget 'AnnotationStrategy'!
ser.write(p, f); // Serialize to a file or whatever you need
最后...
XML 输出:
<parent>
<childs class="java.util.ArrayList">
<child secondValue="1">a</child>
<child secondValue="2">b</child>
<child secondValue="4">d</child>
</childs>
</parent>
只有child
对象 where activated
was的元素true
,那些 withfalse
被跳过。
注意:如果要删除,class="java.util.ArrayList"
请参见此处:删除 class= 属性