我正在尝试使用 JAXB 在 XML 中序列化一些对象,当我到达一个作为抽象类指针的字段时,我得到了这个代码序列化:
<Message> <MessageID>1</MessageID> <OperationType>Update</OperationType> **<Content xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="product">** <SKU>skuparent</SKU> ...
但我需要的是:
<Message> <MessageID>1</MessageID> <OperationType>Update</OperationType> **<Product>** <SKU>skuparent</SKU>
而且我无法使用“@XMLTransient”标记对其进行转换,这是我从其他帖子中得到的唯一建议
我的代码是这样的:
@XmlType(propOrder = { "MessageID", "operationType", "Content"})
public static class message{
public int MessageID;
private String OperationType;
@XmlElement(name ="OperationType")
public String getOperationType() {
return OperationType;
}
public void setOperationType(String _operationType) {
OperationType = operationType.valueOf(_operationType).toString();
}
public AmazonContent Content;
}
其中“AmazonContent”是这样的抽象类:
@XmlSeeAlso({Product.class})
public abstract class AmazonContent {
}
子类实例是:
@XmlRootElement(name = "Product")
@XmlType(propOrder = { "SKU", "StandardProductID", "DescriptionData", "ProductData"})
public class Product extends AmazonContent {
有任何想法吗?