我在使用XStream将xml解组到Object时遇到了麻烦。操作的属性值永远不会设置为我的ImportItem对象中的字段操作。对于包含属性的其他元素,我使用自定义转换器,但我不希望为客户使用客户转换器。这是可能的,我应该如何将操作绑定到Item对象?
XML:
<persons>
<person operation="insert">
<personNumber>196211065104</personNumber>
<startDate>2011-04-13T00:00:00.000+02:00</startDate
<expireDate>2012-04-14T00:00:00.000+02:00</expireDate
<orderDate>2011-04-12T00:00:00.000+02:00</orderDate>
<contact type="name">Joe</name>
<contact type="email">test@test.com</contact>
<contact type="landLine">12345678</contact>
</person>
</persons>
我的工厂看起来像这样:
public class XStreamFactory {
public static XStream instace() {
XStream xs = new XStream();
xs.aliasType("person", ImportItem.class);
xs.useAttributeFor(ImportItem.class, "operation"); <-------
xs.aliasType("persons", ImportBatch.class);
xs.aliasType("contact", ImportContactData.class);
xs.registerConverter(new ImportContactConverter());
xs.registerConverter(new DateConverter());
xs.registerConverter(new ImportOperationConverter()); <-------
xs.addImplicitCollection(ImportBatch.class, "items");
xs.addImplicitCollection(ImportItem.class, "contactData", ImportContactData.class);
return xs;
}
}
批次:
@SuppressWarnings({"UnusedDeclaration"})
public class ImportBatch {
private List<ImportItem> items;
public List<ImportItem> getItems() {
return items;
}
@Override
public String toString() {
return "ImportBatch{" +
"items=\n " + StringUtils.join(items, "\n ") +
"\n}";
}
}
ImportItem:
@SuppressWarnings({"UnusedDeclaration"})
public class ImportItem {
private ImportOperation operation;
private String personNumber;
private Date startDate;
private Date expireDate;
private Date orderDate;
private List<ImportContactData> contactData;
public ImportOperation getOperation() {
return operation;
}
public void setOperation(ImportOperation operation) {
this.operation = operation;
}
public String getCompanyName() {
return companyName;
}
public String getOrganisationNumber() {
return organisationNumber;
}
public String getItescoID() {
return itescoID;
}
public String getAdID() {
return adID;
}
public Date getStartDate() {
return startDate;
}
public Date getExpireDate() {
return expireDate;
}
public String getContactEmail() {
return contactDataFor(email);
}
public String getContactPerson() {
return contactDataFor(businessPerson);
}
public String getContactPhone() {
return contactDataFor(landLine);
}
}
新的ImportOperation代码:&lt; ------
public class ImportOperation {
public enum OperationType{insert, update, delete}
private OperationType operation;
public ImportOperation(OperationType operation) {
this.operation = operation;
}
public OperationType getOperation() {
return operation;
}
public void setOperation(OperationType operation) {
this.operation = operation;
}
}
新转换器代码:&lt; ------
public class ImportOperationConverter implements SingleValueConverter {
public String toString(Object obj) {
return ((ImportOperation)obj).getOperation().toString();
}
public Object fromString(String str) {
return new ImportOperation(ImportOperation.OperationType.valueOf(str));
}
public boolean canConvert(Class clazz) {
return clazz == ImportOperation.class;
}
}
答案 0 :(得分:4)
您需要告诉XStream字段操作是一个xml属性。
添加
xs.useAttributeFor(ImportItem.class, "operation");
原因是,XStream默认将所有内容映射到XML字段。如果解组对象并且未在XML中定义Object字段,则默认情况下XStream会将其设置为NULL。 - 将这两部分放在一起解释行为,并告诉你该怎么做。 - &GT;配置映射,以便将Person.operation映射到属性。