我正在尝试使用简单的 xml (http://simple.sourceforge.net/) 序列化一个对象。对象设置非常简单:
@Root(name = "order_history")
public class OrderHistory {
@Element(name = "id", required = false)
public int ID;
@Element(name = "id_order_state")
public int StateID;
@Element(name = "id_order")
public int OrderID;
}
问题是当我创建一个没有 ID 的此类的新实例时:
OrderHistory newhistory = new OrderHistory();
newhistory.OrderID = _orderid;
newhistory.StateID = _stateid;
我通过简单的xml对其进行序列化:
StringWriter xml = new StringWriter();
Serializer serializer = new Persister();
serializer.write(newhistory, xml);
它仍然在生成的 xml 中读取 0:
<?xml version='1.0' encoding='UTF-8'?>
<order_history>
<id>0</id>
<id_order>2</id_order>
<id_order_state>8</id_order_state>
</order_history>
我猜这是因为 ID 属性不为空,因为整数不能为空。但是我真的需要摆脱这个节点,我宁愿不手动删除它。
任何线索任何人?