我在将 java 对象序列化为 XML 时遇到问题。我的课程如下所示:
@Root(strict = false, name = "Detail")
public class ProductList {
@ElementList(inline = true, entry = "Product")
private List<Product> products;
}
@Root(strict = false)
public class Product {
@Element(name = "ProductCode")
private String productCode;
@Element(name = "ProductPrice")
private double productPrice;
@Element(name = "Currency")
private String currency;
@Element(name = "ConversionRate")
private int conversionRate;
@Element(name = "ProductPoints", required = false)
private int productPoints;
@Element(name = "ProductCount", required = false)
private int productCount;
@Element(name = "ProductName", required = false)
private String productName;
@Element(name = "MinPrice", required = false)
private double minPricet;
@Element(name = "MaxPrice", required = false)
private double maxPricet;
@Element(name = "CanChangePrice", required = false)
private String canChangePrice;
}
下面的 XML 是从服务器发送的,并且反序列化没有任何问题:
<?xml version="1.0" encoding="UTF-8"?>
<Detail>
<Product>
<ProductCode>0001</ProductCode>
<ProductPrice>0.90</ProductPrice>
<Currency>GEL</Currency>
<ConversionRate>200</ConversionRate>
<ProductName>Bread</ProductName>
<MinPrice>0.9</MinPrice>
<MaxPrice>0.9</MaxPrice>
<CanChangePrice>N</CanChangePrice>
</Product>
<Product>
...
</Product>
</Detail>
我尝试生成具有这种结构的 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<Detail>
<Product>
<ProductCode>0001</ProductCode>
<ProductPrice>0.90</ProductPrice>
<Currency>GEL</Currency>
<ConversionRate>200</ConversionRate>
<ProductPoints>180</ProductPoints>
<ProductCount>1</ProductCount>
</Product>
<Product>
...
</Product>
</Detail>
但我明白了:
<Detail>
<Product>
<ProductCode>0001</ProductCode>
<ProductPrice>0.9</ProductPrice>
<Currency>GEL</Currency>
<ConversionRate>200</ConversionRate>
<productPoints>180</productPoints>
<ProductCount>1</ProductCount>
<ProductName>Bread</ProductName>
<MinPrice>0.9</MinPrice>
<MaxPrice>0.9</MaxPrice>
<CanChangePrice>N</CanChangePrice>
</Product>
<Product>
...
</Product>
</Detail>
标签<ProductName>
, <MinPrice>
, <MaxPrice>
,<CanChangePrice>
不得包含在序列化的 XML 中。
有什么方法可以告诉框架在序列化时不要包含特定的标签\变量?