如何使用XStream为相同的类元素设置XML元素的不同别名?
我有以下类,并希望重复使用Phone类来表示将生成以下格式的XML的家庭电话和工作电话
<customer>
<id>222</id>
<name>TestCustomer</name>
<workPhone>
<workPhoneNumber>12345678</workPhoneNumber>
<workPhoneExtn>2345</workPhoneExtn>
</workPhone>
<workPhone>
<workPhoneNumber>23456789</workPhoneNumber>
<workPhoneExtn>2555</workPhoneExtn>
</workPhone>
<homePhone>
<homePhoneNumber>222222222</homePhoneNumber>
<homePhoneExtn>1234</homePhoneExtn>
</homePhone>
</customer>
使用以下代码,我可以设置不同的别名,直到家庭电话和工作电话对象的课程级别。
@XStreamAlias("customer")
public class Customer {
private String id;
private String name;
@XStreamImplicit(itemFieldName = "workPhone")
private ArrayList<Phone> workPhones;
@XStreamImplicit(itemFieldName = "homePhone")
private ArrayList<Phone> homePhones;
}
public class Phone {
private String number;
private String extn;
}
使用上面的类定义,我只能得到以下XML结构:
<customer>
<id>222</id>
<name>TestCustomer</name>
<workPhone>
<number>12345678</number>
<extn>2345</extn>
</workPhone>
<workPhone>
<number>12345678</number>
<extn>2355</extn>
</workPhone>
<homePhone>
<number>222222222</number>
<extn>1234</extn>
</homePhone>
</customer>
我不清楚Mappers或转换器是否有助于实现这一点。
有人可以建议是否有设置电话的号码和分机取代别名“workphoneNumber”,“workphoneExtn”/“homePhoneNumber”,“homePhoneExtn”取决于其类的别名?它应该在编组和解组时起作用。请建议。
答案 0 :(得分:0)
试试这个:
xstream.alias("workPhone", Person.class);
xstream.alias("homePhone", Person.class);
xstream.aliasField("workPhoneNumber", Person.class, "number");
xstraem.aliasField("homePhoneNumber", Person.class, "number");
...