我正在尝试使用SimpleXML 2.1.0将Web服务给出的xml响应映射到java对象,但是我被卡住了。
这是xml:
<QueryINVAP-WS-ASSETResponse baseLanguage="ES" creationDateTime="2016-11-08T17:10:09-03:00">
<INVAP-WS-ASSETSet>
<ASSET rowstamp="265381811">
<ASSETID>1529</ASSETID>
<ASSETNUM>2503</ASSETNUM>
<DESCRIPTION>POWER CHASSIS()</DESCRIPTION>
<ITEMNUM>A000232</ITEMNUM>
<LOCATION>LOCATIONTEST</LOCATION>
<SERIALNUM>123456789</SERIALNUM>
<SITEID>TVD</SITEID>
</ASSET>
</INVAP-WS-ASSETSet>
可以想象,我要转换为Java对象的唯一数据是assetid,assetnum,描述,itemnum,位置,SerialNum和siteid。
到现在为止,这就是我所拥有的:
@Root(name = "QueryINVAP-WS-ASSETResponse", strict = false)
public class Activos {
@ElementList(inline = true, entry = "ASSET", type = Activo.class)
private List<Activo> activos;
@Root
class Activo {
@Attribute(name = "rowstamp")
public String rowstamp;
@Element(name = "ASSETID")
public Integer assetid;
@Element(name = "ASSETNUM")
public String assetnum;
@Element(name = "DESCRIPTION")
public String description;
@Element(name = "LOCATION")
public String location;
@Element(name = "SERIALNUM")
public String serialnum;
@Element(name = "SITEID")
public String siteid;
}
}
我得到的错误是:
org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=ASSET, inline=true, name=, required=true, type=class invap.invapgestionmovil.modelos.Activos$Activo) on field 'activos' private java.util.List invap.invapgestionmovil.modelos.Activos.activos for class invap.invapgestionmovil.modelos.Activos at line 1
我究竟做错了什么?
好吧,终于我解决了这个问题。我创建了三个类:
@Root(name = "QueryINVAP-WS-ASSETResponse", strict=false)
public class QueryINVAPWSASSETResponse {
@Element(name = "INVAP-WS-ASSETSet")
private INVAPWSASSETSet set;
}
@Root(name = "INVAP-WS-ASSETSet", strict=false)
public class INVAPWSASSETSet {
@ElementList(inline=true, name = "ASSET")
private List<Activo> activos;
}
@Root(name = "ASSET", strict = false)
public class Activo {
@Attribute(name = "rowstamp")
public String rowstamp;
@Element(name = "ASSETID")
public Integer assetid;
@Element(name = "ASSETNUM")
public String assetnum;
@Element(name = "DESCRIPTION")
public String description;
@Element(name = "LOCATION")
public String location;
@Element(name = "SERIALNUM")
public String serialnum;
@Element(name = "SITEID")
public String siteid;
}
现在,当我进行呼叫时,我将资产与其他两个类“包装在一起”。
希望这对其他人有帮助:)问候
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句